Possible Duplicate:
C++ Timer function to provide time in nano seconds
High resolution timer with C++ and Linux?
I would like to time a few C functions, and I'm trying to come up with a systematic way of doing so. So far, what I have is the simplest form of timing:
#include <sys/time.h>
struct timeval start_time, end_time;
gettimeofday(&start_time, NULL);
<my function>
gettimeofday(&end_time, NULL);
total_usecs = (end_time.tv_sec-start_time.tv_sec) * 1000000 +
(end_time.tv_usec-start_time.tv_usec);
printf ("parfib,%ld,%d\n",(long)res,total_usecs);
I have also found
timespec time1, time2;
int temp;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time1);
<my function>
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time2);
cout<<diff(time1,time2).tv_sec<<":"<<diff(time1,time2).tv_nsec<<endl;
But even with std=gnu99
I have not been able to get this to work (I copied the above code from a website, but I did translated into C before compiling :). Regardless, I'm wondering if any of you know of docs or blogs or something that discusses timing functions like I'm trying to do. Note, I'm not after profilers such as gprof
or the like, I need this profiler to be hand-made.