用 QueryPerformanceFrequency 和 QueryPerformanceCounter 進行高精度計時

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章