Skip to content

Instantly share code, notes, and snippets.

@plonk
Last active December 14, 2015 15:19
Show Gist options
  • Select an option

  • Save plonk/5106933 to your computer and use it in GitHub Desktop.

Select an option

Save plonk/5106933 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <windows.h>
/* LARGE_INTEGER を表示する */
#define disp_large_int(x) printf(#x " = %lld\n", x.QuadPart)
/* QueryPerformanceFrequency と ...Counter を使って
Sleep(1000) を実行する間にたった時間を表示する */
int main(int argc, char **argv)
{
LARGE_INTEGER freq; // 秒間カウント
LARGE_INTEGER a, b; // 開始時刻、終了時刻
LARGE_INTEGER diff; // 差
// プロセッサの数を限定
SetThreadAffinityMask(GetCurrentThread(), 1);
// 1秒あたりのカウント数を取得
QueryPerformanceFrequency(&freq);
// 計測開始
QueryPerformanceCounter(&a);
// 1秒寝る
Sleep( 1000 );
// 計測終了
QueryPerformanceCounter(&b);
// 差を求める
diff.QuadPart = b.QuadPart - a.QuadPart;
// 結果を表示
disp_large_int(freq);
disp_large_int(a);
disp_large_int(b);
disp_large_int(diff);
printf(" = %f seconds\n", (double) diff.QuadPart / freq.QuadPart);
return 0;
}
/* 出力例:
freq = 3215380
a = 331495678493
b = 331498893132
diff = 3214639
= 0.999770 seconds
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment