我在我的函数中使用chrono::high_resolution_clock::now()
来计算循环花费多长时间来获得x次重复的估计完成时间。但是,当使用future
从main调用时,它会给出错误的时间。我使用过Visual C ++和intel c编译器。怎么解决这个问题?
答案 0 :(得分:0)
这是一个已知的bug in VC和其他人,标题使用的系统时钟分辨率不是很高。如果你想要真正准确的计时,可以使用boost或rdtsc。
使用boost你会像使用std :: chrono一样使用它,链接中的例子:
boost::chrono::high_resolution_clock::time_point t3 = boost::chrono::high_resolution_clock::now();
boost::chrono::high_resolution_clock::time_point t4 = boost::chrono::high_resolution_clock::now();
while (t3==t4 ) {
t4 = boost::chrono::high_resolution_clock::now();
}
boost::chrono::nanoseconds ns = t4 - t3;
rdtsc的可能实现
// On some processors in certain circumstances the value can be off
// on some old processors rdtsc doesn't exist.
// only valid for x86/x64.
// only valid when no thread switch happens between calls.
// will be wrong if cpu uses turbo mode or changes speed.
// ToDO check where. make fallback timer.
// will blow up if you run over the epoch, shouldn't happen as the epoch is ~136 years
// beware of cpu instruction reordering if timing very short functions.
static uint64_t tick() noexcept {
unsigned lo, hi;
asm volatile("rdtsc" : "=a" (lo), "=d" (hi));
return int64_t(hi) << 32 | lo;
}