Measure runtime in C (on Windows)

Is there a better function or way to measure time than the clock() function in Windows? I have a short operation, and when I try clock() or gettickcount() , it says it took 0.0 seconds. I need a way to measure it in milliseconds or nanoseconds.

+2
c windows execution-time
source share
2 answers

You can use QueryPerformanceCounter and QueryPerformanceFrequency :

 #include <stdio.h> #include <stdlib.h> #include <windows.h> int main(void) { LARGE_INTEGER frequency; LARGE_INTEGER start; LARGE_INTEGER end; double interval; QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&start); // code to be measured QueryPerformanceCounter(&end); interval = (double) (end.QuadPart - start.QuadPart) / frequency.QuadPart; printf("%f\n", interval); return 0; } 
+7
source share

You can use QueryPerformanceCounter in combination with QueryPerformanceFrequency to get nanosecond accuracy.

0
source share

All Articles