Skip to content

计时

c++提供了多种计时方式,包括:

  1. clock_t
  2. struct timespec
  3. std::chrono::time_point

一句话总结:clock_t不适用于多核计算环境,会出现非常大的计算误差。使用后两种计时方式即可

clock_t

clock_t start, end;
start = clock();
...
...
end = clock();
std::cout << "image read: " << (double) (start - end) / CLOCKS_PER_SEC << std::endl;
....

timespec

struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
...
...
clock_gettime(CLOCK_MONOTONIC, &end);
auto t1 = (double) (clock_img_read.tv_sec - start.tv_sec) * 1000 +
          (double) (clock_img_read.tv_nsec - start.tv_nsec) / 1000000;
...
...

time_point

int64_t getCurrentLocalTimeStamp() {
    std::chrono::time_point <std::chrono::system_clock, std::chrono::milliseconds> tp = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());

    auto tmp = std::chrono::duration_cast<std::chrono::milliseconds>(tp.time_since_epoch());
    return tmp.count();
}