LARGE_INTEGER lpPerformanceCount, lpFrequency;
QueryPerformanceCounter(&lpPerformanceCount);
QueryPerformanceFrequency(&lpFrequency);
(Count.QuadPart很长很长,显示CPU数量)
(Freq.QuadPart是很长很长,显示Count的频率为一秒钟)
尝试实时打印微秒。
稳定的输出:
printf("%llu\n", ((long double)lpPerformanceCount.QuadPart/ lpFrequency.QuadPart) * 1000000);
错误的输出:(即使乍一看,结果前后前后不一致地跳跃)
printf("%llu\n", 1000000 * (lpPerformanceCount.QuadPart / lpFrequency.QuadPart) + (lpPerformanceCount.QuadPart % lpFrequency.QuadPart));
编辑:printf需要在其输入中进行进一步的(无符号long long)转换,原始代码具有由func的返回值完成的转换。
最佳答案
您确定%llu
输出合理的两倍吗?lpPerformanceCount.QuadPart / lpFrequency.QuadPart
给您时间,四舍五入为整秒。lpPerformanceCount.QuadPart % lpFrequency.QuadPart
为您提供滴答计数(自上一整秒以来的滴答计数)。
在时间上加上一个计数可以使您..如何礼貌地把它...废话。
我总是使用双重算术,更不用说麻烦了。但是,如果您坚持使用非FPU代码,则可以使用:
count.QuadPart*1000000 / (freq.QuadPart*1000000)
它将溢出得更快(尽管这不是我认为的实际问题)。将其修正为整数运算:
count.QuadPart / freq.QuadPart
+ (count.QuadPart % freq.QuadPart) * 1000000 / freq.QuadPart
(我希望这是正确的...)