大头龙仔Blog

A strong man can save himself. A great man can save another.

写了个高精度时间计算器的struct

| Comments

感觉经常要测程序的性能,就写了个简单的结构体,方便自己以后调用。简单起见,没有处理QueryPerformanceCounter与QueryPerformanceFrequency调用失败的情况。

#include “stdafx.h”
#include <Windows.h>
#include <stdio.h>

struct HighPreTimer
{
private:
    
LARGE_INTEGER timeStart;
    
LARGE_INTEGER timeEnd;
    
LARGE_INTEGER cpuFreq;

public:
    
HighPreTimer()
    
{   
        
memset(this, 0, sizeof(HighPreTimer));
 
    
}


    
double GetTime()
    
{
        
QueryPerformanceFrequency( &cpuFreq );
        
return (double)(timeEnd.QuadPart - timeStart.QuadPart) * 1000 / (double)cpuFreq.QuadPart;
    
}

    
void Start()
    
{ 
        
QueryPerformanceCounter( &timeStart  span>);
 
    
}


    
void End()
    
{ 
        
QueryPerformanceCounter( &timeEnd );
 
    
}

};


int main(int argc, char* argv[])
{
    
HighPreTimer timer;

    
timer.Start();  
    
Sleep(2000);

    
timer.End();

    
printf(“%f second\n”, timer.GetTime() / 1000 );
    
return 0;
}

Comments