【发布时间】:2012-12-06 03:30:25
【问题描述】:
可能重复:
Getting cpu cycles using RDTSC - why does the value of RDTSC always increase?
Get CPU cycle count?
我想编写分析排序算法的 C++ 代码,我需要知道对数组进行排序需要多少处理器周期。
关于如何做到这一点的任何建议?
我找到了这个代码here:
uint64_t rdtsc(){
unsigned int lo,hi;
__asm__ __volatile__ ("rdtsc" : "=a" (lo), "=d" (hi));
return ((uint64_t)hi << 32) | lo;
}
我知道它是内联汇编,有人能解释一下它是如何工作的以及如何使用它吗?
我运行 Linux。我的电脑是双核的,有区别吗?
【问题讨论】:
-
为什么不像普通人那样测量挂壁时间?
-
如果您使用 rdtsc,您并不是在“分析”排序算法,而是在凭经验衡量这些算法的特定实现的性能。
-
@JanDvorak - 不知道你在说什么“正常”的人;我认为很多关心性能的程序员使用 rdtsc 或类似的。
-
请阅读Getting cpu cycles using RDTSC - why does the value of RDTSC always increase?的答案。另外,请注意 rdtsc 始终假定您在同一处理器上运行。
-
Jasse's Good 的评论解决了这个问题,感谢您的建议。
标签: c++ optimization inline-assembly cpu-cycles