Over a million developers have joined DZone.

Code Execution Timer

DZone's Guide to

Code Execution Timer

· ·
Free Resource
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: */

/* 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.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}