现在的位置: 首页 > 综合 > 正文

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

2012年03月04日 ⁄ 综合 ⁄ 共 2660字 ⁄ 字号 评论关闭
转自:http://blog.csdn.net/actionli/archive/2007/07/06/1681595.aspx 

有许多专门的测试工具,测试的准确性很高,本文说的是一些简单的测试方法,这些方法多数是记录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);  
  }

抱歉!评论已关闭.