Don't bother like my brother

Just another WordPress weblog

 

Benchmarking your C code September 11, 2009

Filed under: code — Artem @ 7:57 pm

Ever wondered how long does it take for a piece of your code to execute? It is easy to find out the execution time using the gettimeofday function from the <sys/time.h>. The code is the following:

#include <sys/time.h>

struct timeval t1, t2;
double tdiff;

gettimeofday(&t1, NULL);

<your code here>

gettimeofday(&t2, NULL);
tdiff = (t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) / 1000000.0;
printf("Execution time: %2.5f\n", tdiff);
 

Leave a Reply