I needed to calculate the time it takes to run a certain function and ran into the following code,record & output the execution time of a piece of code in nanoseconds
And also is there any difference between
struct timeval timer_spent
& timeval timer_spent
/* Put this line at the top of the file: */
#include <sys/time.h>
/* 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);
printf("Time spent: %.6f\n", timer_spent)/1000000.0;
But I need the precise timing in nanoseconds. I've no complete idea about the timeval struct.
So kindly help me guys.. !!