next up previous

6.3 Timing an Execution

The hwclock(), dclock() and mclock() system calls can be used to obtain timing information. dclock() is the most useful one. It reads the hardware counter and returns a double precision time in seconds. The actual time returned by these calls is usually unimportant. To time an interval in a program, the difference in the time between an initial call and final call to dclock() is used.

Here we measure the time taken by a loop. Node 0 prints the time to the screen.

double start_time, end_time, elapsed_time;
int i, imax;
start_time = dclock();
for (i=0; i<imax; i++) {

end_time = dclock();
elapsed_time = end_time - start_time;
if (mynode() == 0)
printf("Time taken = %8.6lfn", elapsed_time);