程序的运行效率很重要,为了明确到底是那一块代码浪费时间,浪费多少时间,检测一下是很有必要的,用下面的方法可以精确地统计时间。第一种精确到秒,第二种精确到毫秒,第三种精确到0.000001秒,大家可以根据自己的需求选用
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
int main()
{
time_t t1,t2;
time(&t1);
Sleep(1000);
time(&t2);
printf("%d %d %d秒\n",t1,t2,t2-t1);
clock_t c1,c2;
c1=clock();
Sleep(100);
c2=clock();
printf("%d %d %d毫秒\n",c1,c2,c2-c1);
LARGE_INTEGER litmp;
LONGLONG start, end;
double dft, dff, dfm;
QueryPerformanceFrequency(&litmp);
dff = (double) litmp.QuadPart;
QueryPerformanceCounter(&litmp);
start = litmp.QuadPart;
Sleep(1000);
QueryPerformanceCounter(&litmp);
end = litmp.QuadPart;
dfm = (double) (end - start);
dft = dfm / dff;
printf("%lf毫秒\n",dfm/dff*1000);
}