![The C Standard Library Reference](/c_standard_library/images/c-mini-logo.jpg)
- The C Standard Library
- C Library - Home
- C Library - <assert.h>
- C Library - <complex.h>
- C Library - <ctype.h>
- C Library - <errno.h>
- C Library - <fenv.h>
- C Library - <float.h>
- C Library - <inttypes.h>
- C Library - <iso646.h>
- C Library - <limits.h>
- C Library - <locale.h>
- C Library - <math.h>
- C Library - <setjmp.h>
- C Library - <signal.h>
- C Library - <stdalign.h>
- C Library - <stdarg.h>
- C Library - <stdbool.h>
- C Library - <stddef.h>
- C Library - <stdio.h>
- C Library - <stdlib.h>
- C Library - <string.h>
- C Library - <tgmath.h>
- C Library - <time.h>
- C Library - <wctype.h>
- C Standard Library Resources
- C Library - Quick Guide
- C Library - Useful Resources
- C Library - Discussion
- C Programming Resources
- C Programming - Tutorial
- C - Useful Resources
C library - clock() function
The C library clock() function returns the number of clock ticks elapsed since the program was launched. To get the number of seconds used by the CPU, we 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.
The CLOCKS_PER_SEC is a type of macros that is used for building time programs.
Syntax
Following is the syntax of the C library clock() function −
clock_t clock(void)
Parameters
- This function doesn't accept any parameter.
Return Value
This function returns the number of clock ticks elapsed since the start of the program. On failure, it returns a value of -1.
Example 1
Following is the basic C library program to see the demonstration of clock() function.
#include <time.h> #include <stdio.h> int main () { clock_t start_t, end_t; double 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: %f\n", total_t ); printf("Exiting of the program...\n"); return(0); }
The above code produces the following result −
Starting of the program, start_t = 1194 Going to scan a big loop, start_t = 1194 End of the big loop, end_t = 26796 Total time taken by CPU: 0.025602 Exiting of the program...
Example 2
In this example, we demonstrates the measurement of processor time in seconds for various operations using the clock() function.
#include <stdio.h> #include <time.h> #include <math.h> int main() { float a; clock_t time_req; // Measure time for multiplication time_req = clock(); for (int i = 0; i < 200000; i++) { a = log(i * i * i * i); } time_req = clock() - time_req; printf("Processor time taken for multiplication: %f seconds\n", (float)time_req / CLOCKS_PER_SEC); // Measure time using pow function time_req = clock(); for (int i = 0; i < 200000; i++) { a = log(pow(i, 4)); } time_req = clock() - time_req; printf("Processor time taken in pow function: %f seconds\n", (float)time_req / CLOCKS_PER_SEC); return 0; }
Output
On execution of above code, we get the following result−
Processor time taken for multiplication: 0.005235 seconds Processor time taken in pow function: 0.010727 seconds