hi i asked few days back if it was possible to count the execution time using clock, well i got replies telling that its not possible
my code runs on linux, are there any specific functions that can be used only in linux in order count the execution time in nanoseconds? and then get the seconds?
for example if we have a running time in nanoseconds
33939
i want 33939*10^-9 = 0.000033939 seconds
i ve researched a little bit and i found that the clock_gettime can do the job
but i havent found any source code on how to do this if you could help me with a simple sample code or suggest me something else it would be appreciated
i have found another one for gettimeofday
Code:
#include <iostream>
#include <sys/time.h>
using namespace std;
int main(void)
{
timeval tim;
gettimeofday(&tim, NULL);
double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
int i,j=0;
for(i=0;i<1000;i++){
j=j+1;
}
gettimeofday(&tim, NULL);
double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
cout<<t2-t1<<" seconds elapsed\n";
}
gives output
5.00679e-06 seconds elapsed
is this correct?? but it is not in nanoseconds