How do I measure time in C?

snakile picture snakile · Aug 24, 2010 · Viewed 69k times · Source

I want to find out for how long (approximately) some block of code executes. Something like this:

startStopwatch();
// do some calculations
stopStopwatch();
printf("%lf", timeMesuredInSeconds);

How?

Answer

KLee1 picture KLee1 · Aug 24, 2010

You can use the clock method in time.h

Example:

clock_t start = clock();
/*Do something*/
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC;