Small and simple code snippet for measuring CPU time (both user and system) in C:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| #include <stdio.h> #include <time.h> void tik() { static clock_t start, end; static int flag = 0; static double elapsed_time; if (flag == 0) { start = clock(); } else { end = clock(); elapsed_time = (double)(end - start) / (double)CLOCKS_PER_SEC; printf("%f\n", elapsed_time); } flag = 1 - flag; }
|
Usage:
1 2 3 4 5 6 7 8
| #include "./tik.h" int main(int argc, char **argv) { tik(); int count = 1 << 25; for (int i = 0; i < count; ++i) ; tik(); }
|