用 QueryPerformanceFrequency 和 QueryPerformanceCounter 进行高精度计时

来源:互联网 发布:淘宝优衣库代购推荐 编辑:程序博客网 时间:2024/05/16 07:45
void main() {   
    LARGE_INTEGER lv;

    // 获取每秒多少CPU Performance Tick
    QueryPerformanceFrequency( &lv );

    // 转换为每个Tick多少秒
    double secondsPerTick = 1.0 / lv.QuadPart;
   
    for ( size_t i = 0; i < 100; ++i ) {
        // 获取CPU运行到现在的Tick数
        QueryPerformanceCounter( &lv );

        // 计算CPU运行到现在的时间
        // 比GetTickCount和timeGetTime更加精确
        double timeElapsedTotal = secondsPerTick * lv.QuadPart;
       
        cout.precision( 6 );
        cout << fixed << showpoint << timeElapsedTotal << endl;
        //printf( "%lf /n", timeElapsedTotal ) ;
    }
}
http://www.cppblog.com/bidepan2023/archive/2008/01/22/41627.html
原创粉丝点击