C++ 计算代码运行时间的几种方法(转)

有许多专门的测试工具,测试的准确性很高,本文说的是一些简单的测试方法,这些方法多数是记录CPU的运行时间,没有考虑操作系统的分时复用,不过不太严格的情况都可一用。

1. #include <time.h>
long start=clock(),end(0);
//ToDo:process code
end=clock();
long result=(end-start)/1000

2. windows 平台
#include <windows.h>
double start=getticktime(),end(0);
//ToDo:process code
end=getticktime();
double result=end-start;

3.windows 平台
#include <windows.h>
    LARGE_INTEGER frequency,start,end;  
    QueryPerformanceFrequency(&frequency);  
    QueryPerformanceCounter(&start);  
//ToDO:process code
    QueryPerformanceCounter(&end);  
    double   d   =   (double)(end.QuadPart   -   start.QuadPart)   /   (double)frequency.QuadPart   *   1000.0;

4.根据线程而来的
CThreadTime   ElapsedTime;  
ElapsedTime.BeginGetElapsedTime();  
   
//TODO:   Your   performance   code  
   
int   nThreadTine   =   ElapsedTime.EndGetElapsedTime();  
   
   
该类的实现如下:  
//   This   class   is   for   getting   the   elapsed   thread   time   of   the   CPU,   the   unit   is   ms  
//   the   usage   is:    
//    
//   CThreadTime   ElapsedTime;  
//   ElapsedTime.BeginGetElapsedTime();  
//   TODO:   Your   performance   code  
//   int   nThreadTine   =   ElapsedTime.EndGetElapsedTime();  
//  
   
   
#include   <Windows.h>  
   
class   CThreadTime  
{  
public:          
          void         BeginGetElapsedTime();  
          __int64   EndGetElapsedTime();  
   
private:  
          __int64   FileTimeToQuadWord(PFILETIME   pft);  
   
private:  
          FILETIME   ftKernelTimeStart;  
          FILETIME   ftKernelTimeEnd;  
          FILETIME   ftUserTimeStart;  
          FILETIME   ftUserTimeEnd;  
          FILETIME   ftDummy;  
};  
   
//   Get   the   time   elapsed   since   the   thread   start  
inline   void   CThreadTime::BeginGetElapsedTime()  
{  
          GetThreadTimes(GetCurrentThread(),   &ftDummy,   &ftDummy,   &ftKernelTimeStart,   &ftUserTimeStart);  
}  
   
//   Calculate   the   time   elapsed    
inline   __int64   CThreadTime::EndGetElapsedTime()  
{  
          GetThreadTimes(GetCurrentThread(),   &ftDummy,   &ftDummy,   &ftKernelTimeEnd,   &ftUserTimeEnd);  
   
          __int64   qwKernelTimeElapsed   =   FileTimeToQuadWord(&ftKernelTimeEnd)   -   FileTimeToQuadWord(&ftKernelTimeStart);  
          __int64   qwUserTimeElapsed   =   FileTimeToQuadWord(&ftUserTimeEnd)   -   FileTimeToQuadWord(&ftUserTimeStart);  
   
          //   Get   total   time   duration   by   adding   the   kernel   and   user   times.  
          //   the   default   is   100ns,   so   we   convert   it   to   ms  
          return   (qwKernelTimeElapsed   +   qwUserTimeElapsed)   /   10000;  
}  
   
inline   __int64   CThreadTime::FileTimeToQuadWord(PFILETIME   pft)    
{  
          return   (Int64ShllMod32(pft->dwHighDateTime,   32)   |   pft->dwLowDateTime);  
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值