Windows下精确获取程序运行时间

 

#include  < windows.h >
#include 
< iostream >
using   namespace  std;


class  CGetPeriodTime
{
private:
    union UTIME
    
{
       LARGE_INTEGER _li;
       unsigned 
long long  _time;
    }
;
    
class InitFreQueuency
    
{
       
public:
           unsigned 
long long s_Frequency;
           InitFreQueuency();
    }
;
    
static InitFreQueuency s_InitFreQueuency; 
    UTIME _PeriodTime;

public:
    
void Begin(){QueryPerformanceCounter(&_PeriodTime._li);}
    unsigned 
long long End();
}
;

CGetPeriodTime::InitFreQueuency CGetPeriodTime::s_InitFreQueuency;

inline CGetPeriodTime::InitFreQueuency::InitFreQueuency()
{
    UTIME li;
    QueryPerformanceFrequency(
&li._li);
    s_Frequency
=li._time;
}


unsigned 
long   long   CGetPeriodTime::End()
{
    UTIME li;
    QueryPerformanceCounter(
&li._li);
    _PeriodTime._time
=li._time-_PeriodTime._time;
    
return (_PeriodTime._time*1000)/s_InitFreQueuency.s_Frequency;
}


int  main()
{
    CGetPeriodTime time;
    time.Begin();
    Sleep(
1000);
    cout
<<time.End()<<endl;
    
return 0;

}

你可能感兴趣的:(C/C++)