Code:
#include <time.h>
#include <stdio.h>
#include <sys/time.h>
int
timeval_subtract (result, x, y)
struct timeval *result, *x, *y;
{
/* Perform the carry for the later subtraction by updating Y. */
if (x->tv_usec < y->tv_usec) {
int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
y->tv_usec -= 1000000 * nsec;
y->tv_sec += nsec;
}
if (x->tv_usec - y->tv_usec > 1000000) {
int nsec = (x->tv_usec - y->tv_usec) / 1000000;
y->tv_usec += 1000000 * nsec;
y->tv_sec -= nsec;
}
/* Compute the time remaining to wait.
`tv_usec' is certainly positive. */
result->tv_sec = x->tv_sec - y->tv_sec;
result->tv_usec = x->tv_usec - y->tv_usec;
/* Return 1 if result is negative. */
return x->tv_sec < y->tv_sec;
}
int main(int argc, char *argv[])
{
clock_t start, end;
double cpu_time_used;
struct timespec echodelay;
start = clock();
int i = 0;
int z = 0;
for(i= 0;i<1000000000;i++)
{
z++;
if (z > 1000)
z = 0;
}
end = clock();
cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
//cpu_time_used = ((double) (end - start)) /CLK_TCK;
printf("start: %e\n", start);
printf("end: %e\n",end);
timeval_subtract (&echodelay, &end, &start);
int sec = echodelay.tv_sec;
int nsec = echodelay.tv_nsec;
printf("Echo delay is %ds and %d ns\n", sec, echodelay.tv_nsec);
printf("CPU: %f\n",cpu_time_used);
return 0;
}
At last I got it.
Echo delay is 4551208s and 478712 ns
This is 4.551208478712 sec?