Code Execution Timer
Join the DZone community and get the full member experience.Join For Free
These two bits of code can be used to record & output the execution time of a piece of code in microseconds. NOTE: The functions used here are only available on BSD-like systems (I think).
/* Put this line at the top of the file: */ #include
/* Put this right before the code you want to time: */ struct timeval timer_start, timer_end; gettimeofday(&timer_start, NULL); /* Put this right after the code you want to time: */ gettimeofday(&timer_end, NULL); double timer_spent = timer_end.tv_sec - timer_start.tv_sec + (timer_end.tv_usec - timer_start.tv_usec) / 1000000.0; printf("Time spent: %.6f\n", timer_spent);
Opinions expressed by DZone contributors are their own.