Skip to content

Instantly share code, notes, and snippets.

@t-mat
Created April 13, 2021 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t-mat/81654fd0faa883d3fa297baab7717bf4 to your computer and use it in GitHub Desktop.
Save t-mat/81654fd0faa883d3fa297baab7717bf4 to your computer and use it in GitHub Desktop.
[WIN32]Casual benchmark of WIN32 time measurement APIs
// Casual microbenchmark of WIN32 time measurement APIs
//
// This micro-benchmark compares some WIN32 time measurement APIs.
// It calls each API for 128 million times and shows actual consumed
// time in milli-seconds including overhead of loop, etc.
//
// Result:
// clock() , dt= 4863.0996 ms, N=134217728, tx=0x0000004c321019a3
// GetTickCount64() , dt= 198.9263 ms, N=134217728, tx=0x0052290d9540cbfa
// QueryPerformanceCounter() , dt= 1745.0657 ms, N=134217728, tx=0x896882087bc31bb8
// timeGetTime() , dt= 1332.4894 ms, N=134217728, tx=0x0052295bd97d9ec7
// GetSystemTimeAsFileTime() , dt= 235.2870 ms, N=134217728, tx=0x538df18ccbae43ef
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdint.h> // uint32_t
#include <time.h> // clock()
#include <timeapi.h> // timeGetTime()
#include <functional> // std::function<>
#pragma comment(lib, "Winmm.lib") // timeGetTime()
constexpr uint32_t N = 1024 * 1024 * 128;
uint64_t test_clock() {
uint64_t t = 0;
for(uint32_t i = 0 ;i < N; ++i) {
t += clock();
}
return t;
}
uint64_t test_GetTickCount64() {
uint64_t t = 0;
for(uint32_t i = 0 ;i < N; ++i) {
t += GetTickCount64();
}
return t;
}
uint64_t test_QueryPerformanceCounter() {
uint64_t t = 0;
for(uint32_t i = 0 ;i < N; ++i) {
LARGE_INTEGER li;
QueryPerformanceCounter(&li);
t += li.QuadPart;
}
return t;
}
uint64_t test_timeGetTime() {
uint64_t t = 0;
for(uint32_t i = 0 ;i < N; ++i) {
t += timeGetTime();
}
return t;
}
uint64_t test_GetSystemTimeAsFileTime() {
uint64_t t = 0;
for(uint32_t i = 0 ;i < N; ++i) {
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
t += (static_cast<uint64_t>(ft.dwHighDateTime) << 32) | ft.dwLowDateTime;
}
return t;
}
int main() {
const auto b = [](const char* s, const std::function<uint64_t()>& f) {
LARGE_INTEGER t0, t1, freq;
QueryPerformanceCounter(&t0);
const uint64_t tx = f(); // use return value to avoid optimization
QueryPerformanceCounter(&t1);
QueryPerformanceFrequency(&freq);
const int64_t dt = static_cast<int64_t>(t1.QuadPart - t0.QuadPart);
const double ms = dt * 1000.0 / static_cast<double>(freq.QuadPart);
printf("%-26s, dt=%10.4f ms, N=%d, tx=0x%016zx\n", s, ms, N, tx);
};
b("clock()" , [&]() { return test_clock(); });
b("GetTickCount64()" , [&]() { return test_GetTickCount64(); });
b("QueryPerformanceCounter()" , [&]() { return test_QueryPerformanceCounter(); });
b("timeGetTime()" , [&]() { return test_timeGetTime(); });
b("GetSystemTimeAsFileTime()" , [&]() { return test_GetSystemTimeAsFileTime(); });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment