windows和Linux系统给程序计时的代码段

在window系统下编程时,测试某段程序代码运行时间的方法:

#include 

LARGE_INTEGER startCount;
LARGE_INTEGER endCount;
LARGE_INTEGER freq;
double time_ms = 0.0;

//检索时钟频率
QueryPerformanceFrequency(&freq);
//开始
QueryPerformanceCounter(&startCount);
//待检测运行时间的代码段
QueryPerformanceCounter(&endCount);
//单位是ms
time_ms = (double)(endCount.QuadPart-startCount.QuadPart)/fre.QuadPart * 1000;

在linux系统下编程时,测试某段程序代码运行时间的方法:

#include 

double cpuSecond()
{
    struct timeval tp;
    gettimeofday(&tp,NULL);
    return ((double)tp.tv_sec+(double)tp.tv_usec*1.e-6);
}

double iStart,iElaps;
iStart = cpuSecond();
//待测的程度代码
iElaps = cpuSecond()-iStart;
printf("%f ms",iElaps*1000);

 

 

 

你可能感兴趣的:(windows和Linux系统给程序计时的代码段)