windows下的C/C++精确计时

时间:2014-10-04 20:11:27   收藏:0   阅读:196

由于我要测试线性筛法的速度,用上了C/C++精确计时.此时传统的clock()方法不够用了,我们需要另一种测量的办法,即CPUTicks/CPUFreq.如何实现呢?

#include <windows.h>
LARGE_INTEGER freq,start,stop;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&start);
//这样便得到一个CPUTick
//do some stuff....
QueryPerformanceCounter(&stop);
//注意LARGE_INTEGER是一个union起32bit low,high和64bit Quad的东西
//那么
double timeused=(double)(stop.QuadPart-start.QuadPart)/(double)freq.QuadPart;

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!