2008年11月19日星期三

VC中的高精度计时并输出到日志

void main() {  
 double gl_Frequency;
double gl_BeginTime, gl_CurrentTime;
double gl_TimeEllapsed;
// 重定向输出目标为标准输出
wxLog *logger = new wxLogStderr(/*&cout*/);
// 激活为当前输出目标
wxLog::SetActiveTarget(logger);
    // 获取每秒多少CPU Performance Tick
      QueryPerformanceFrequency((LARGE_INTEGER *)&gl_Frequency);
// 获取当前时刻
QueryPerformanceCounter( (LARGE_INTEGER *)&gl_BeginTime );

    // 转换为每个Tick多少秒
    double secondsPerTick = 1.0 / gl_Frequency;
   
    for ( size_t i = 0; i < 100; ++i ) {
        // 获取CPU运行到现在的Tick数
        QueryPerformanceCounter((LARGE_INTEGER *)&gl_CurrentTime);
        // 计算CPU运行到现在的时间
        // 比GetTickCount和timeGetTime更加精确
      gl_TimeEllapsed = (gl_CurrentTime - gl_BeginTime)/gl_Frequency;

           
        cout.precision( 6 );
        cout << fixed << showpoint << gl_TimeEllapsed  << endl;
        //printf( "%lf \n", timeElapsedTotal ) ;
    }
// 输出到日志
wxLogMessage(_T("%f"), gl_TimeEllapsed);
}

没有评论: