QueryPerformanceCounterとは何か
QueryPerformanceCounter
は、Windows APIの一部であり、高解像度のパフォーマンスカウンターの現在の値を取得するための関数です。この関数は、マイクロ秒単位での時間計測が可能で、これにより、プログラムのパフォーマンス計測やベンチマーキングに非常に役立ちます。
QueryPerformanceCounter
関数は、以下のように使用します:
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);
// Activity to be timed
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
//
// We now have the elapsed number of ticks, along with the
// number of ticks-per-second. We use these values
// to convert to the number of elapsed microseconds.
// To guard against loss-of-precision, we convert
// to microseconds *before* dividing by ticks-per-second.
//
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
- このコードでは、
QueryPerformanceCounter
関数を使用して、特定のアクティビティの開始時と終了時のタイムスタンプを取得し、その差分を計算してアクティビティの所要時間をマイクロ秒単位で計算しています。このように、QueryPerformanceCounter
関数は、高精度なタイムスタンプを取得するための強力なツールとなります。 - https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
- https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
- https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
- https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
- https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
- https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
- https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
- https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
- https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
- https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
QueryPerformanceCounterの使用方法
QueryPerformanceCounter
関数は、以下のように使用します:
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
// パフォーマンスカウンターの周波数を取得
QueryPerformanceFrequency(&Frequency);
// アクティビティの開始時刻を取得
QueryPerformanceCounter(&StartingTime);
// タイムスタンプを取得したいアクティビティをここに記述
// アクティビティの終了時刻を取得
QueryPerformanceCounter(&EndingTime);
// 経過時間を計算
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
// 経過時間をマイクロ秒単位に変換
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
- このコードでは、
QueryPerformanceCounter
関数を使用して、特定のアクティビティの開始時と終了時のタイムスタンプを取得し、その差分を計算してアクティビティの所要時間をマイクロ秒単位で計算しています。このように、QueryPerformanceCounter
関数は、高精度なタイムスタンプを取得するための強力なツールとなります。 - https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
- https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
- https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
- https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
- https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
- https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
- https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
- https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
- https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
- https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
QueryPerformanceCounterのパラメーターと戻り値
QueryPerformanceCounter
関数は、以下の形式で定義されています:
BOOL QueryPerformanceCounter(
_Out_ LARGE_INTEGER *lpPerformanceCount
);
この関数は1つのパラメーターを取ります:
– lpPerformanceCount
: これは出力パラメーターで、関数が成功すると、このパラメーターは現在のパフォーマンスカウンターの値に設定されます。
戻り値はBOOL
型で、関数が成功した場合はTRUE
を、失敗した場合はFALSE
を返します。関数がFALSE
を返した場合、具体的なエラーコードを取得するためにGetLastError
関数を使用できます。
- したがって、
QueryPerformanceCounter
関数を使用する際は、適切なエラーハンドリングを行うことが重要です。 -
https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
-
https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
-
https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
-
https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
-
https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
QueryPerformanceCounterを用いた高解像度タイムスタンプの取得
QueryPerformanceCounter
関数を使用して高解像度のタイムスタンプを取得する方法は以下の通りです:
// パフォーマンスカウンターの周波数とタイムスタンプを格納するための変数を定義
LARGE_INTEGER Frequency, StartingTime, EndingTime, ElapsedMicroseconds;
// パフォーマンスカウンターの周波数を取得
QueryPerformanceFrequency(&Frequency);
// アクティビティの開始時刻を取得
QueryPerformanceCounter(&StartingTime);
// タイムスタンプを取得したいアクティビティをここに記述
// アクティビティの終了時刻を取得
QueryPerformanceCounter(&EndingTime);
// 経過時間を計算
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
// 経過時間をマイクロ秒単位に変換
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
- このコードでは、
QueryPerformanceCounter
関数を使用して、特定のアクティビティの開始時と終了時のタイムスタンプを取得し、その差分を計算してアクティビティの所要時間をマイクロ秒単位で計算しています。このように、QueryPerformanceCounter
関数は、高精度なタイムスタンプを取得するための強力なツールとなります。 -
https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
-
https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
-
https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
-
https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
-
https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
QueryPerformanceCounterのエラーハンドリング
QueryPerformanceCounter
関数は、成功するとTRUE
を、失敗するとFALSE
を返します。したがって、この関数を使用する際は、適切なエラーハンドリングを行うことが重要です。
以下に、QueryPerformanceCounter
関数のエラーハンドリングの例を示します:
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
// パフォーマンスカウンターの周波数を取得
if(!QueryPerformanceFrequency(&Frequency))
{
// エラーハンドリング
std::cerr << "QueryPerformanceFrequency failed with error: " << GetLastError() << std::endl;
return 1;
}
// アクティビティの開始時刻を取得
if(!QueryPerformanceCounter(&StartingTime))
{
// エラーハンドリング
std::cerr << "QueryPerformanceCounter failed with error: " << GetLastError() << std::endl;
return 1;
}
// タイムスタンプを取得したいアクティビティをここに記述
// アクティビティの終了時刻を取得
if(!QueryPerformanceCounter(&EndingTime))
{
// エラーハンドリング
std::cerr << "QueryPerformanceCounter failed with error: " << GetLastError() << std::endl;
return 1;
}
// 経過時間を計算
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
// 経過時間をマイクロ秒単位に変換
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
- このコードでは、
QueryPerformanceCounter
関数が失敗した場合にエラーメッセージを出力し、プログラムを終了します。このように、QueryPerformanceCounter
関数を使用する際は、適切なエラーハンドリングを行うことが重要です。 -
https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
-
https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
-
https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
-
https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
-
https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps
QueryPerformanceCounterの実用例
以下に、QueryPerformanceCounter
関数を使用して、特定のコードブロックの実行時間を計測する実用例を示します:
#include <windows.h>
#include <iostream>
int main()
{
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicroseconds;
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency);
QueryPerformanceCounter(&StartingTime);
// ここに時間を計測したいコードを書く
for(int i = 0; i < 1000000; i++) {}
QueryPerformanceCounter(&EndingTime);
ElapsedMicroseconds.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicroseconds.QuadPart *= 1000000;
ElapsedMicroseconds.QuadPart /= Frequency.QuadPart;
std::cout << "Elapsed time: " << ElapsedMicroseconds.QuadPart << " microseconds." << std::endl;
return 0;
}
- このコードは、1から1,000,000までの整数を単純にカウントするループの実行時間をマイクロ秒単位で計測します。このように、
QueryPerformanceCounter
関数は、特定のコードブロックの実行時間を高精度で計測するための強力なツールとなります。 -
https://docs.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter
-
https://www.codeproject.com/Articles/2116/Timing-Method-for-Windows
-
https://stackoverflow.com/questions/1739259/how-to-use-queryperformancecounter
-
https://www.codeguru.com/cpp/w-p/system/timers/article.php/c5759/Using-the-High-Performance-Counter.htm
-
https://docs.microsoft.com/en-us/windows/win32/sysinfo/acquiring-high-resolution-time-stamps