Updated April 5, 2023
Introduction to clock() Function
A clock function is used to determine the processing time of executing a function or a set of codes. For using the clock function in C, we have to use the header file <time.h>. This header file needs to be included in the program. A value of type clock_t is returned by the function, which has to be divided by the clock_per_sec value in order to calculate the processing time in seconds. It needs to be called at the very beginning of the program and even at the end of the program, and the difference between both of the values is the actual time spent by the processor on the program.
Syntax of clock() Function
In the header file in C language, it is called as:
#include<time.h>
While in the C++ language, it is called as:
#include<ctime>
Below is the syntax used to in C and C++ coding languages:
clock_t T(void)
Working of clock() Function
- The major use of the clock function is to return the time elapsed by the processor from the start of the program till its end.
- The time is represented in clock ticks by the function.
- To represent the time in seconds, we have to take the value received by the clock function and divide it by clocks_per_sec.
- If in case there is an error in calculating the elapsed time or if the time is not available or the time can’t be represented, then the value returned by the clock function would be -1.
- For using the function in C, one has to use the time.h header file in the program.
- Moreover, the function can be used in the ANSI/ISO 9899-1990 version of the C language.
Examples of clock() Function
In the below examples, the return function returns the number of clock ticks elapsed since the program is executed. On failed execution, the value of -1 is returned by the function.
Example #1
In the below example, we have used C time to return the number of clock ticks elapsed since the code was executed, and these values are converted to seconds by dividing by a macro CLOCKS_PER_SEC. This is defined in <time.h> in the header file.
Code:
#include <time.h>
#include <stdio.h>
int main () {
clock_t beggning, thanos_snap, whole;
int A;
beggning = clock();
printf("Initial days started from = %ld \n \n", beggning);
printf("Had the happy feeling for = %ld days \n \n", beggning);
for(A=1; A< 2000000; A++) {
}
thanos_snap = clock();
printf("Everything ended within = %ld days \n \n", thanos_snap);
whole = (double)(thanos_snap - beggning) / CLOCKS_PER_SEC;
printf("Time as whole taken for a change %f \n \n", whole );
printf("Ending everything here...\n");
return(0);
}
Output:
Example #2
This example explains how we can calculate the time taken for executing different sets of code. This is really important when one has to execute a very large code and time is important for the executions. Here in this example, we have taken different variables for calculations which are EDUCBA, A, ADD, B, M, which are all of the integer types. Moreover, for calculating time, four variables are created, which are C1, C2, C3, C4.
Now, first of all, we declare the C1 variable as a function. Here we, also display that when the timer is set, and the C1 variable is divided by clocks per second to get the time in seconds. In the second phase of the code, we have created a loop that runs until the variable is less than the variable M, with B getting incremented by 1 in each cycle. Now, the C2 variable is declared as a function that calculates the time for completing one cycle. Again, the time is calculated in seconds. Then, one more variable is declared as a function which is C4; now, this variable calculates the time taken in completing one cycle of the second loop.
Code:
#include<stdio.h>
#include<time.h>
void main()
{
double EDUCBA, A = 2.9;
int ADD;
int B, M = 2000000;
clock_t C1, C2, C3, C4;
C1 =clock();
printf ( "Initiated at = %lf seconds \n \n", (double)C1/ (double)CLOCKS_PER_SEC);
for ( B=0 ; B<M; B++)
ADD +=1;
C2 =clock();
printf("Per second time is = %d seconds \n \n", CLOCKS_PER_SEC);
printf("To completed 1 cycle it take = %lf seconds \n \n", (double)(C2 - C1) /(double) CLOCKS_PER_SEC );
C3 =clock();
for ( B=0 ; B<M; B++)
EDUCBA += A;
C4 =clock();
printf("Second cycle is completed in = %lf seconds \n \n",(double)(C4 -C3)/(double)CLOCKS_PER_SEC );
}
Output:
Example #3
In this example, we are calculating the number of times the program gets to run and the time taken for running the program, number of times we specified in the first place.
Code:
#include <stdio.h>
#include <time.h>
#include <math.h>
int EDUCBA (int A) {
int M,N;
int repeat=A-1;
for (M=2
; M<=A
; M++) for (N=sqrt(M)
;N>1
;N--) if (M%N==0) {repeat++
; break;}
return repeat;
}
int main ()
{
clock_t T;
int X;
T = clock();
printf ("**Loading the program**\n \n");
X = EDUCBA (1000000);
printf ("The data packets collected are loading are : %d\n \n",X);
T = clock() - T;
printf ("I executed the program %d times internally (it means %f seconds taken by me). \n", T ,((float) T)/CLOCKS_PER_SEC);
return 0;
}
Output:
Conclusion
On the basis of the above article, we understood the concept of clock function in the C language. The clock function is highly important if we want to calculate the time being taken by the processor to execute a program. This article would help in making the most of the clock function in their programs.
Recommended Articles
We hope that this EDUCBA information on “clock() Function” was beneficial to you. You can view EDUCBA’s recommended articles for more information.