Home >>C Time Library Function >C time clock() function
The C time clock() function is used to return the number of clock ticks elapsed since the program was launched. To convert these values to seconds we need to be divided by a macro CLOCKS_PER_SEC. It is defined in <time.h> header file.
Syntax:clock_t clock(void)Here is an example of clock() function:
#include <stdio.h>
#include <time.h>
int main ()
{
clock_t start_t, end_t, total_t;
int i;
start_t = clock();
printf("Starting of the program, start_t = %ld\n", start_t);
printf("Going to scan a big loop, start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("End of the big loop, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("Total time taken by CPU: %ld\n", total_t );
printf("Exiting of the program...\n");
return(0);
}