`clock_t`

, defined in `CLOCKS_PER_SEC`

, also defined in
The program listed below illustrates the
simple use of the `clock()` function. The program compares the CPU
time required to raise a `double` to the fourth power via a direct calculation
and via a call to the `pow()` function. Actually, both operations are performed
a million times and the elapsed CPU time is then divided by a million.

/* timing.c */ /* Program to test operation of clock() function */ #include <time.h> #include <math.h> #define N_LOOP 1000000 int main() { int i; double a = 11234567890123456.0, b; clock_t time_1, time_2; time_1 = clock(); for (i = 0; i < N_LOOP; i++) b = a * a * a * a; time_2 = clock(); printf ("CPU time needed to evaluate a*a*a*a: %f microsecs\n", (double) (time_2 - time_1) / (double) CLOCKS_PER_SEC); time_1 = clock(); for (i = 0; i < N_LOOP; i++) b = pow(a, 4.); time_2 = clock(); printf ("CPU time needed to evaluate pow(a, 4.): %f microsecs\n", (double) (time_2 - time_1) / (double) CLOCKS_PER_SEC); return 0; }The typical output from this program is as follows:

CPU time needed to evaluate a*a*a*a: 0.190000 microsecs CPU time needed to evaluate pow(a, 4.): 1.150000 microsecs %Clearly, evaluating a fourth power using the