Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

C++ 精確計時:QueryPerformanceFrequency 與 QueryPerformanceCounter 用法

QueryPerformanceFrequency 是什麼

類型:Win32 API

原型:BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

作用:取得硬體支援的高精度計時器頻率。

返回值:非零代表硬體支援高精度計時器;零代表不支援、讀取失敗。

在 Windows 上,QueryPerformanceFrequency() 與 QueryPerformanceCounter() 是一對高精度計時器,前提是機器硬體要支援,使用時需引入 windows.h。

BOOL QueryPerformanceFrequency(LARGE_INTEGER *lpFrequency);

BOOL QueryPerformanceCounter (LARGE_INTEGER *lpCount);

資料型別 LARGE_INTEGER 可以是 8 位元組的整數,也可以是兩個 4 位元組整數組成的 union,取決於編譯器是否支援 64 位元:

typeef union _ LARGE_INTEGER
{
struct
{
DWORD LowPart;
LONG HighPart;
};
LONGLONG QuadPart;
} LARGE_INTEGER;

使用順序是:先呼叫 QueryPerformanceFrequency() 取得計時器時鐘頻率,接著在要計時的事件前後各呼叫一次 QueryPerformanceCounter(),兩次計數相除頻率,就得到事件經歷的精確時間。

測試 Sleep 的實際耗時

#include <stdio.h>

#include <windows.h>

void main()
{
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    double time;

    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);

    Sleep(1000);

    QueryPerformanceCounter(&nEndTime);
    time=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;

    printf("%f\n",time);
    Sleep(1000);

system("Pause");
}

實例:比較遞迴與迭代版 Fibonacci

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

unsigned long Fib1(unsigned long n)
{
    return (n == 1 || n == 2) ? 1 : Fib1(n - 1) + Fib1(n - 2);
}

unsigned long Fib2(unsigned long n)
{
    if (n == 1 || n == 2)
    {
        return 1;
    }
    unsigned long m1 = 1, m2 = 1;
    for (unsigned long i = 3; i <= n; i++)
    {
        m2 = m1 + m2;
        m1 = m2 - m1;
    }

    return m2;
}

int main()
{
    LARGE_INTEGER nFreq;

    LARGE_INTEGER nBeginTime;

    LARGE_INTEGER nEndTime;

    double time1 = 0, time2 = 0;
    unsigned long result1 = 0;
    unsigned long result2 = 0;
    unsigned long fib_num = 45;
    cout.precision(10);
    cout.setf(cout.showpoint); //設置為始終輸出小數點後的數字,就是說 a = 3,它也輸出 3.00000 這
    QueryPerformanceFrequency(&nFreq);

    QueryPerformanceCounter(&nBeginTime);

    result1 = Fib1(fib_num);

    QueryPerformanceCounter(&nEndTime);

    time1=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;

    cout<<"Fib1: "<< fixed<<setprecision(8)<<setiosflags(ios::showpoint)<<time1<<endl;

    QueryPerformanceFrequency(&nFreq);

    QueryPerformanceCounter(&nBeginTime);

    result2 = Fib2(fib_num);

    QueryPerformanceCounter(&nEndTime);

    time2=(double)(nEndTime.QuadPart-nBeginTime.QuadPart)/(double)nFreq.QuadPart;

    cout<<"Fib2: "<< fixed<<setprecision(20)<<setiosflags(ios::showpoint)<<time2<<endl;

    cout<<"Fib2 cost "<<time2*(unsigned long)100/time1<<"% time of Fib1."<<endl;
    //cout << "Hello world!" << endl;
    return 0;
}

指數級的遞迴版本與線性的迭代版本一比較,差距會非常明顯——這也正好說明為什麼「先量測、再優化」永遠比憑感覺猜要可靠。若需要跨平台計時,C++11 的 <chrono> 標準庫是更 portable 的選擇。

Copyright © Linux教程網 All Rights Reserved