VC++ 获取代码段执行时间
代码1:精确到秒
double tstart,tend,tget; tstart=GetTickCount(); //程序段 tend=GetTickCount();//只能精确到秒 tget=tend-tstart;
代码2:精确到秒
clock_t t_begin,t_end; double trun; t_begin=clock(); //只能精确到秒 for(i=0;i<=10000;i++) k+=1; t_end=clock(); trun=(double)(t_end-t_begin); */
代码3:可以精确到微秒
LARGE_INTEGER litmp; LONGLONG QPart1,QPart2; double dfMinus, dfFreq, dfTim; QueryPerformanceFrequency(&litmp); dfFreq = (double)litmp.QuadPart;// 获得计数器的时钟频率 QueryPerformanceCounter(&litmp); QPart1 = litmp.QuadPart;// 获得初始值 for(i=0;i<=5000;i++) k+=1; QueryPerformanceCounter(&litmp); QPart2 = litmp.QuadPart;//获得中止值 dfMinus = (double)(QPart2-QPart1); dfTim = dfMinus/dfFreq;// 获得对应的时间值,单位为秒