The C library functions to measure computational time.
The clock() function is of type clock_t (defined
in time.h), therefore you need to include in your C program:
-
#include <time.h>
clock() returns the number of clock ticks of elapsed processor time
(or -1 if there is an error). To know how many clock ticks are in one second,
you need to use the macro CLOCKS_PER_SEC (defined also in time.h).
The standard procedure of using clock() is to take the differences
between different time measurements, and divide them by CLOCKS_PER_SEC.
Here is an example:
-
#include <time.h>
-
...
clock_t t1, t2;
t1 = clock();
...
/* operations you want to measure */
...
t2 = clock();
printf(" Timing is %f\n", (double)(t2-t1)/CLOCKS_PER_SEC );
...
Also, you can measure the computational time using the function gettimeofday
defined in the header file of sys/time.h
-
#include <sys/time.h>
-
...
struct timeval start, end;
double diff;
...
gettimeofday(&start,0);
...
/* operations you want to measure */
...
gettimeofday(&end,0);
diff = (end.tv_sec - start.tv_sec) +
(double)(end.tv_usec - start.tv_usec)/1e6;
printf("Timing is %g sec.\n", diff);
...