程序运行时间的测量
算法的好坏有好多评价指标,其中一个重要的指标是时间复杂度。如果两个程序完成一个同样的任务,即功能相同,处理的数据相同,那么运行时间较短者为优。操作系统和库函数一般都提供了对时间测量的函数,这么函数一般都会返回一个代表当前时间的数值,通过在运行某个程序或某段代码之前调用一次时间函数来得到一个数值,在程序或者代码段运行之后再调用一次时间函数来得到另一个数值,将后者减去前者即为程序的运行时间。
在windwos平台(指windwow95及以后的版本,下同),常用的用于测量时间的函数或方法有三种:1.API函数GetTickCount或C函数clock, 2.API函数QueryPerformanceCounter, 3:汇编指令RDSTC
1.API函数GetTickCount:
函数原形:DWORD GetTickCount(VOID);
该函数取回从电脑开机至现在的毫秒数,即每个时间单位为1毫秒。他的分辨率比较低,常用在测量用时较长程序,如果你的程序用时为100毫秒以上,可以使用这个函数.另一个和GetTickCount类似的函数是clock,该函数的回的时间的单位的是CLOCKS_PER_SEC,在windows95/2000操作系统,该值是1000,也就是说,在windows平台,这两个函数的功能几乎完全相同。
2.API函数QueryPerformanceCounter:
函数原形:BOOL QueryPerformanceCounter( LARGE_INTEGER *lpPerformanceCount);该函数取回当前的高分辨值performance计数器,用一个64bit数来表示。如果你的硬件不支持高分辨performance计数器,系统可能返加零。不像GetTickCount,每个计数器单位表示一个固定的时间1毫秒,为了知道程序确切的执行时间,你需要调用函数QueryPerformanceFrequency来得到每秒performance计数器的次数,即频率。
3.汇编指令RDTSC:
RDTSC 指令读取cpu内部的“时间戳(TimeStamp)",它是一个64位无符号数计数器,这条指令执行完毕后,存储在EDX:EAX寄存中。该指令从intel奔腾cpu开始引入,一些老式的cpu不支持该指令,奔腾后期的cpu包括AMD的cpu均支持这条指令。和QueryPerformanceCounter类似,要想知道程序的确实的执行时间,必须知道cpu的频率,即平常所说的cpu的主频。不幸的是没有现成的函数可以得到cpu的频率。一种办法可行的办法延时一段指定时间,时间的测量可以用QueryPerformanceCounter来做,在这段时间的开始和结束调用RDTSC,得到其时钟数的差值,然后除以这段时间的的秒数就可以了。
下面的代码给出使用3个函数封装和测试代码,用RDTSC指令来计时的代码参考了一个Ticktest的源代码,作者不详。
getTime1,使用GetTickCount返回一个表示当前时间的值,单位秒。
getTime2,和getTime1类似,精度更高。
getTime3,返回一个64bit的一个计数器,欲转换为秒,需除以cpu频率。示例代码见函数test3.
#include "stdafx.h" #include "windows.h" #include "tchar.h" double getTime1() { DWORD t=GetTickCount(); return (double)t/1000.00; } double getTime2() //使用高精度计时器 { static LARGE_INTEGER s_freq; LARGE_INTEGER performanceCount; double t; if (s_freq.QuadPart==0) { if ( !QueryPerformanceFrequency( &s_freq)) return 0; } QueryPerformanceCounter( &performanceCount ); t=(double)performanceCount.QuadPart / (double)s_freq.QuadPart; return t; } void test1() { double t1,t2; t1=getTime1(); Sleep(1000); t2=getTime1()-t1; printf("It take %.8f second/n",t2); } void test2() { double t1,t2; t1=getTime2(); Sleep(1000); t2=getTime2()-t1; printf("It take %.8f second/n",t2); } inline BOOL isNTOS() //检测是否运行在NT操作系统 { typedef BOOL (WINAPI *lpfnGetVersionEx) (LPOsveRSIONINFO); static int bIsNT=-1; if (bIsNT!=1) return (BOOL)bIsNT; // Get Kernel handle HMODULE hKernel32 = GetModuleHandle(_T("KERNEL32.DLL")); if (hKernel32 == NULL) return FALSE; #ifdef _UNICODE lpfnGetVersionEx lpGetVersionEx = (lpfnGetVersionEx) GetProcAddress(hKernel32,_T("GetVersionExW")); #else lpfnGetVersionEx lpGetVersionEx = (lpfnGetVersionEx) GetProcAddress(hKernel32,_T("GetVersionExA")); #endif if (lpGetVersionEx) { OsveRSIONINFO osvi; memset(&osvi,sizeof(OsveRSIONINFO)); osvi.dwOsversionInfoSize = sizeof(OsveRSIONINFO); if (!lpGetVersionEx(&osvi)) bIsNT=FALSE; else bIsNT=(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT); } else { //Since GetVersionEx is not available we kNown that //we are running on NT 3.1 as all modern versions of NT and //any version of Windows 95/98 support GetVersionEx bIsNT=TRUE; } return bIsNT; } inline static BOOL checkRDSTC() //检测cpu是否支持RDSTC指令 { static int bHasRDSTC= -1; SYstem_INFO sys_info; if ( bHasRDSTC !=-1 ) return (BOOL)bHasRDSTC; GetSystemInfo(&sys_info); if (sys_info.dwProcessorType==PROCESSOR_INTEL_PENTIUM) { try { _asm { _emit 0x0f ; rdtsc _emit 0x31 } } catch (...) // Check to see if the opcode is defined. { bHasRDSTC=FALSE; return FALSE; } // Check to see if the instruction ticks accesses something that changes. volatile ULARGE_INTEGER ts1,ts2; _asm { xor eax,eax _emit 0x0f ; cpuid _emit 0xa2 _emit 0x0f ; rdtsc _emit 0x31 mov ts1.HighPart,edx mov ts1.LowPart,eax xor eax,eax _emit 0x0f ; cpuid _emit 0xa2 _emit 0x0f ; rdtsc _emit 0x31 mov ts2.HighPart,edx mov ts2.LowPart,eax } // If we return true then there's a very good chance it's a real RDTSC instruction! if (ts2.HighPart>ts1.HighPart) bHasRDSTC=TRUE; else if (ts2.HighPart==ts1.HighPart && ts2.LowPart>ts1.LowPart) bHasRDSTC=TRUE; else { printf("RDTSC instruction NOT present./n"); bHasRDSTC=FALSE; } } else bHasRDSTC=FALSE; return bHasRDSTC; } //*********************************************** void getTime3( LARGE_INTEGER *pTime) //返加当前cpu的内部计数器 { if (checkRDSTC()) { volatile ULARGE_INTEGER ts; //on NT don't bother disabling interrupts as doing //so will generate a priviledge instruction exception if (!isNTOS()) _asm cli //---------------- _asm { xor eax,eax //-------------save rigister push ebx push ecx _emit 0x0f ; cpuid - serialise the processor _emit 0xa2 //------------ _emit 0x0f ; rdtsc _emit 0x31 mov ts.HighPart,edx mov ts.LowPart,eax pop ecx pop ebx } //----------------- if (!isNTOS()) _asm sti //--------- pTime->QuadPart=ts.QuadPart; } else pTime->QuadPart=0; } // maxDetermainTime:最大测定时间,单位毫秒,在首次调用该函数时, // 将花费maxDetermineTime的时间来测定cpu频率,以后的调用将直接返加静态变量的值 double GetcpuFrequency(DWORD maxDetermineTime ) { static double cpu_freq; LARGE_INTEGER period,t1,t2; register LARGE_INTEGER goal,current; if (cpu_freq>1000) //this value have been initilization return cpu_freq; if (!QueryPerformanceFrequency(&period) || !checkRDSTC()) { cpu_freq=-1.00; return cpu_freq; } QueryPerformanceCounter(&goal); goal.QuadPart += period.QuadPart * maxDetermineTime/1000; getTime3( &t1); //开始计时 do //延时maxDetermineTime毫秒 { QueryPerformanceCounter(¤t); } while(current.QuadPart<goal.QuadPart); getTime3(&t2); //结束计时 cpu_freq=double((t2.QuadPart-t1.QuadPart)*1000/maxDetermineTime); char buff[100]; sprintf(buff,"Estimated the processor clock frequency =%gHz/n",cpu_freq); ::MessageBox(NULL,buff,"",MB_OK); return cpu_freq; } void test3() { LARGE_INTEGER t,t2; double f1,f2; GetcpuFrequency(100); //花费0.1秒时间计算cpu频率 f1=getTime2(); getTime3(&t1); Sleep(1000); getTime3(&t2); f2=getTime2(); t.QuadPart=t2.QuadPart-t1.QuadPart; printf("It take %.8f second by getTime3/n",(double)t.QuadPart/GetcpuFrequency(100)); printf("It take %.8f second by getTime2/n",f2-f1); } int main(int argc,char* argv[]) { test1(); test2(); test3(); return 0; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。