Timing of Programs - Testing Tools

When you are testing your software, you may find that the program is taking too much time for execution. Before optimizing the code, you need to study how much time each segment of the program is taking. To measure the time taken to execute a portion of a program is important for many applications such as embedded software development, communication protocol development etc. This is useful while carrying out simulation studies of algorithms to be implemented on the embedded system. In addition, the timing analysis gives valuable information as to which portions of the code are taking longer time, so that the code can be optimized, or a more efficient algorithm can be chosen to improve the speed.

In C language, the functions time() and clock() are used to measure the time taken by the program segments. We will illustrate the use of these functions through two examples. Listing shows the use of time() function.

Listing ttiming.c 7

Here are the important aspects of this program:

  • You need to include the file time.h
  • Time_t is a structure which stores the time in number of seconds that elapsed since 00:00:00 hours of GMT on 1st January 1970. The significance of this time and date is that Unix was born at that time.
  • Just before doing some heavy computation, you need to call the time() function which returns the time in seconds since the birth of Unix.
  • Then do some computation—here we did square root calculation and taking the inverse of it in a loop.
  • After the computation, again call the time function, it returns the seconds elapsed since the birth of Unix.
  • Now calculate the difference in the times, that is the time taken for the computation. You can compile the program using the command:

You can compare the CPU time obtained through the time function and the time command of the shell. They will be approximately same.

The following program, given in Listing 10.2 illustrates the clock() function call. Note that clock does not give the time in seconds, but in clock ticks. The number of ticks in a second is obtained using the parameter CLOCKS_PER_SEC.

Listing ctimfng.c

This program is similar to the preceding program in which we used the time function. Here are the differences:

  • The clock_t returns the value in integer, not long int.
  • Call the clockQ function just before the computation
  • Call the cloek() function after the computation
  • Calculate the difference in the return values and then divide by CLOCKS_PER_SEC to obtain the number of seconds for doing the computation.

The timings obtained from the clock function and the shell command time will be approximately the same.

Note: While doing peiformance testing of software, you may realize that the execution time for the total program is very high. You need to find out which portions of the program are taking long time. For thiS' the timing functions are of immense use.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Testing Tools Topics