clock
The clock() function is a way to measure the time spent by a section of a program. The sample program shown in Listing 1.2, called sampleclock, measures two for loops. The first for loop is on line 27 of the sampleclock program, and the second is on line 69. The delay_time on lines 17 and 56 calculates how long the clock () call takes. The makefile shown in Listing 1.1 can be used to build the sampleclock program.
Listing 1.1 The Makefile for the sampleclock Program
Makefile for sampleclock program CC = g++ CFLAGS = -g -Wall sampleclock: sampleclock.cc $(CC) $(CFLAGS) sampleclock.cc -o sampleclock clean: rm -f *.o sampleclock
Listing 1.2 sampleclock.cc
1 #include <iostream> 2 #include <ctime> 3 using namespace std; 4 5 // This sample program uses the clock() function to measure 6 // the time that it takes for the loop part of the program 7 // to execute 8 9 int main() 10 { 11 clock_t start_time ,finish_time; 12 13 // get the delay of executing the clock() function 14 15 start_time = clock(); 16 finish_time = clock(); 17 double delay_time = (double)(finish_time - start_time); 18 19 cout<<"Delay time:"<<(double)delay_time<<" seconds."<<endl; 20 21 // start timing 22 23 start_time = clock(); 24 25 // Begin the timing 26 27 for (int i = 0; i < 100000; i++) 28 { 29 cout <<"In:"<<i<<" loop" << endl; 30 } 31 32 // End the timing 33 34 // finish timing 35 36 finish_time = clock(); 37 38 // compute the running time without the delay 39 40 double elapsed_iter_time = (double)(finish_time - start_time); 41 elapsed_iter_time -= delay_time; 42 43 // convert to second format 44 45 double elapsed_time = elapsed_iter_time / CLOCKS_PER_SEC; 46 47 // output the time elapsed 48 49 cout<<"Elapsed time:"<<(double)elapsed_time<<" seconds."<<endl; 50 51 // get the delay of executing the clock() function 52 53 54 start_time = clock(); 55 finish_time = clock(); 56 delay_time = (double)(finish_time - start_time); 57 58 cout<<"Delay time:"<<(double)delay_time<<" seconds."<<endl; 59 60 // now see what results we get by doing the measurement 61 // of the loop by cutting the loop in half 62 63 // start timing 64 65 start_time = clock(); 66 67 // Begin the timing 68 69 for (int i = 0; i < 50000; i++) 70 { 71 cout <<"In:"<<i<<" loop" << endl; 72 } 73 74 // End the timing 75 76 // finish timing 77 78 finish_time = clock(); 79 80 // compute the running time without the delay 81 82 elapsed_iter_time = (double)(finish_time - start_time); 83 elapsed_iter_time -= delay_time; 84 85 // convert to second format 86 87 elapsed_time = elapsed_iter_time / CLOCKS_PER_SEC; 88 89 // output the time elapsed. 90 91 cout<<"Elapsed time:"<<(double)elapsed_time<<" seconds."<<endl; 92 93 return 0; 94 95 }
The sampleclock.cc program can be built by executing the make command.
Figure 1.6 shows the building and running of the sampleclock program.
Figure 1.6 Building and running sampleclock.
Figure 1.7 shows the elapsed time for the first loop as 3.11 seconds.
Figure 1.7 The timing for loop 1.
Figure 1.8 shows the elapsed time for the second loop as 1.66 seconds.
Figure 1.8 The timing for loop 2.
So the sampleclock program takes 3.11 seconds to execute the first for loop of 100000 and 1.66 seconds for the second for loop of 50000, which is very close to half of the time. Now let’s look at another API called gettimeofday that can also be used to time functions in a program.