C Programming

Clock() Function in C Language

In this Linux Hint article, we will show you how to use the clock() function, one of the resources available in the C language, to measure the execution times of a particular process or thread.

Although these fractions of time seem insignificant, there are critical cases where the ability to measure and calculate these small fractions of time are critical factors in the accuracy of the system or process itself. We will give a theoretical description of how clock() works, explain its operation and the units of measurement it uses for this purpose.

Then, using practical examples that include code fragments and images, we will see how you can implement this function in a clear and detailed way to measure the critical times in real-time processes in different systems.

Clock() Function Syntax:

clock_t clock (void)

Clock() Function Description in C Language

The clock() function is empty when called. It contains no input arguments and returns the number of ticks of the clock in “clock_t” at the time of the call.

The pulse count starts at zero when the application is started and continues until the user or the system exits it, resetting to zero by overflow in approximately every 72 minutes. The clock() function does not modify or have control over this counter; it only gets its value when called.

To measure the total execution time of the program, we need to call the clock() only once at the end of the program. To measure the time elapsed from one point of the program to another, we need to call the clock( ) function and calculate the two obtained data.

The calculation to determine the elapsed ticks between two calls of the clock() function is done by subtracting the result of the first call from the result of the second call. Let us look at an example to determine the amount of the elapsed time from one point of the program to another.

The clock() function is defined in “time.h” header functions. We must include it in our “.c” or “.h” code file, as shown in the following image, in order to use it.

#include <time.h>

 

How to Get the Elapsed Clock Ticks from One Point in the Program to Another with the Clock() Function

In this example, we will see how to obtain the number of elapsed ticks from one point in the program to another. These two points correspond to one of the two calls to the clock() function, respectively. To understand this, let’s see the following code:

 

 

#include <stdio.h>

#include <time.h>

 

void main ()

 

   {

   clock_t ticks_ini, ticks_end;

  double  ticks;

   ticks_ini =  clock(); //measure start

   printf ("ticks init measure  %ld\n",  ticks_ini);

   for (int a=0; a<=456450;  a++);

 

   ticks_end = clock(); //measure stop

  ticks = ticks_end - ticks_ini;

   printf ("ticks end measure  %ld\n",  ticks_end );

   printf ("ticks elapsed between measure  %f\n",  ticks);

   return;

}

 

 

First, we create the two variables, ticks_ini and ticks_end, in which we store the result of clock() in its two calls. We calculate it to get the number of elapsed ticks and the integer ticks, in which we store the final result of the total elapsed ticks.

Then, we call the clock() function in our “main” and retrieve the previously defined clock ticks in the ticks_ini variable that elapsed since the start of the program until the first call to this function. We use the printf() function to display this value.

After this delay, which we created with for, we call the clock() function for a second time to get the number of ticks up to this point. We output the result to the screen with the printf() function. We then get the result of the exact number of ticks that elapsed between the first and second calls to clock() by subtracting ticks_ini from ticks_end and storing the result in the variable ticks, which we output to the console with printf().

This way, we get the ticks that elapsed from one point to another in the code.

How to Convert the Number of Ticks Obtained to Seconds with the Clock() Function

Once we have the number of ticks that elapsed since the program started or from one point to another, we can convert this time expressed in ticks to seconds by dividing the result of the previous example by the predefined constant in time.h  CLOCKS _PER_ SEC, as shown in the following snippet:

ticks = (ticks_end - ticks_ini)/(double) CLOCKS_PER_SEC;

printf  ("ticks elapsed in seconds between measure  %f\n",  ticks);

Conclusion

In this Linux Hint article, we showed you how to implement the clock() function to make the time measurements in ticks of the system clock. We also explained how you can measure all or part of these times within the running application. We showed you how to convert the results to seconds. We hope that you found this article useful. You can find more tips about the C language in our articles which you can find using the website’s search engine.

About the author

Julio Cesar

Julio Cesar is a 42 years old programmer with 8 years of experience in embedded systems development, 6 years developing firmware for user interfaces in C and C++. Additionally he has 2 years of experience developing scripts for network devices and 3 years as developer of high frequency PCB (Printed Circuit Board).