Sunday, September 2, 2012

Get real clock time in C to nanosecs approximation

struct timespec start,end;

clock_gettime (CLOCK_REALTIME, &start);
<add code here>
clock_gettime (CLOCK_REALTIME, &end);
float time= (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1000000000;

This code snippet would give you the time taken to execute the added code in real time.

Dont forget to compile your code with a -lrt flag. Not including this flag could  give you a ld error for not finding clock_gettime function.

No comments:

Post a Comment