Full Stack Developer
The C library function clock_t clock(void) returns the number of clock ticks elapsed since the program was launched. To get the number of seconds used by the CPU, you will need to divide by CLOCKS_PER_SEC.
On a 32 bit system where CLOCKS_PER_SEC equals 1000000 this function will return the same value approximately every 72 minutes.
There is a proper syntax that represents the clock() function by returning some approximate processor time which further gets consumed by a program. Depending upon the clock time allocation of resources with each of the operating systems gets allocated. Syntax representation is as follows:
clock_k clock(void)
Where clock_k signifies the function with keyword clock and parameter to return void type.
Time_0 func (time.h)
<image>
# include <time.h>
clock_h strt, end;
double cpu_time_usd;
start=clock();
// Perform Work
end = clock();
cpu_time_usd = ( (double) (end-start)) // This gives clock value on per second basis…
There are some examples mentioned below.
This program demonstrates how the time is consumed by function for its processing as shown in the output below.
#include <stdio.h>
#include <time.h>
void func_1()
{
printf("func_1() starts the flow... \n");
printf("Press enter_button to stop func_1 when entered.. \n");
while(1)
{
if (getchar())
break;
}
printf("func_1() gets an end here.. \n");
}
int main()
{
clock_t t_0;
t_0 = clock();
func_1();
t_0 = clock() - t_0;
double time_consumed = ((double)t_0)/CLOCKS_PER_SEC;
printf("func_1() took %f seconds for execution of some value.. \n", time_consumed);
return 0;
}
Output:
Explanation:
Clock() function in C here is used for demonstrating the flow where func_1 consumes the flow of time with the execution of some value and its time as well. It takes some time for execution and allocates resources as per the operating system. If pressed enter then it will get halted with the required value as shown in the output.
This function demonstrates the time taken by fib_time(20) for consumption of time within the processor for any manipulation as shown in the output.
#include<stdio.h>
int fib_time(int a_0)
{
if (a_0 <= 1)
return a_0;
return fib_time(a_0-1) + fib_time(a_0-2);
}
int main ()
{
printf("The number coming out of fib_time is: %d", fib_time(20));
return 0;
}
Output:
Explanation:
In this program, the number coming out of fib_time comes as the final compiled time of the fib_time() function that is used for the overview it is the final compilation time.
This program demonstrates the current time as part of ctime standard library to determine the current time as part of clock() function for comparison.
#include <stdio.h>
#include <time.h>
int main () {
time_t currnt_time;
time(&currnt_time);
printf("Current_time comes_out to be: = %s", ctime(&currnt_time));
return(0);
}
Output:
Difftime is another c reference function that is also similar to clock() function but with some of the major differences which are depicted in the output below.
#include <stdio.h>
#include <time.h>
int main ()
{
time_t tm_1,tm_2;
char get_inpt [258];
double diff_sc;
time (&tm_1);
printf ("Enter name of_fav food: ");
gets (get_inpt);
time (&tm_2);
diff_sc = difftime (tm_2,tm_1);
printf ("It took almost %.2lf seconds_for typing the time...\n", diff_sc );
return 0;
}
Output:
Explanation: This program demonstrates the function diff_sc with some of the time differences in comparison and it helps in understanding the instantaneous time for switching or any other functionality to work. Here one option is given with entering the name of fav food and to compare and fetch the instance of time with a difference concerning the operating system as shown in the output.
Clock() function in C plays an important role as it helps developers gain insight into the timing constraints concerning the current system or processor in use. It gives developers the ability to differentiate and troubleshoot even if some of the patches and releases are made with help of a check and run this clock() and ctime function as part of the same standard library for verification.