在C语言中或者VC中有很多种计算代码执行时间的代码,其中有一种精确计算时间的方法:
__int64 Frequency;
__int64 startTime = 0;
__int64 curTime;
double dTime = 0;
double dTime = 0;
//QueryPerformanceFrequency返回硬件支持的高精度计数器的频率
//返回值:非零,硬件支持高精度计数器;零,硬件不支持,读取失败
QueryPerformanceFrequency((LARGE_INTEGER *) &Frequency);
QueryPerformanceCounter((LARGE_INTEGER*) &startTime);//开始时间
//需计算执行时间的代码
QueryPerformanceCounter((LARGE_INTEGER*) &curTime);//结束时间
if(startTime> 0 && curTime > 0 )//计算时间,单位是秒
{
dTime = (double)(curTime - startTime) / (double)Frequency;
}
QueryPerformanceFrequency((LARGE_INTEGER *) &Frequency);
QueryPerformanceCounter((LARGE_INTEGER*) &startTime);//开始时间
//需计算执行时间的代码
QueryPerformanceCounter((LARGE_INTEGER*) &curTime);//结束时间
if(startTime> 0 && curTime > 0 )//计算时间,单位是秒
{
dTime = (double)(curTime - startTime) / (double)Frequency;
}
转载于:https://blog.51cto.com/shuiqing/296871