LARGE_INTEGER lpPerformanceCount, lpFrequency;
QueryPerformanceCounter(&lpPerformanceCount);
QueryPerformanceFrequency(&lpFrequency);
(Count.QuadPart is a long long showing a CPU count)
(Freq.QuadPart is a long long showing frequency of Count for a second)
Attempting to print microseconds in real time.
stable output:
printf("%llu\n", ((long double)lpPerformanceCount.QuadPart/ lpFrequency.QuadPart) * 1000000);
erratic output: (result jumps incoherently front and back even if it’s at first glance sane)
printf("%llu\n", 1000000 * (lpPerformanceCount.QuadPart / lpFrequency.QuadPart) + (lpPerformanceCount.QuadPart % lpFrequency.QuadPart));
EDIT: printf needed a further (unsigned long long) conversion in its input, the original code had that done by a return value of a func.
Are you sure
%lluprints a reasonable double?lpPerformanceCount.QuadPart / lpFrequency.QuadPartgives you a time, rounded to full seconds.lpPerformanceCount.QuadPart % lpFrequency.QuadPartgives you a tick count (number ticks since last full second).Adding a count to a time gives you.. how to put that politely… crap.
I alway use the double arithmetics, much less hassle. However, if you insist in non-FPU code, you could use:
which would overflow faster (though not a practical problem I’d assume). Fixing that up for integer arithmetics:
(I hope that’s right…)