clock() Function in C

What is clock() in C?

The clock() function in C is used to measure the CPU time consumed by the program. It is declared in the <time.h> header file.

When you call clock(), it returns the number of clock ticks that have elapsed since the program started execution. To convert this value into seconds, you need to divide it by the constant CLOCKS_PER_SEC.

Why Use clock()?

You can use clock() when you want to:

  • Measure the performance of your code.
  • Time how long a function takes to execute.
  • Compare different algorithms based on execution speed.

Syntax of clock()

#include <time.h>
 
clock_t clock(void);
  • Return Type: clock_t (an integer type that represents clock ticks)
  • Header File: <time.h>
  • Returns: Number of clock ticks since the start of the program.

To get time in seconds:

advertisement
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;

Example: Measuring Execution Time

#include <stdio.h>
#include <time.h>
 
int main()
{
    clock_t quizStart, quizEnd;
    double quizTime;
 
    // Start timing
    quizStart = clock();
 
    // Simulate some task
    for (long i = 0; i < 100000000; i++);
 
    // End timing
    quizEnd = clock();
 
    // Calculate total time taken
    quizTime = (double)(quizEnd - quizStart) / CLOCKS_PER_SEC;
 
    printf("Quiz Execution Time: %.6f seconds\n", quizTime);
    return 0;
}

The output of the program will vary depending on your system’s speed and current CPU load, but here’s a sample output you might see when you run the code:

Note: Join free Sanfoundry classes at Telegram or Youtube
Quiz Execution Time: 0.752347 seconds

This C program measures how long a task takes to run using the clock() function. It records the start time, runs a loop to simulate work, then records the end time. The difference between the two times is divided by CLOCKS_PER_SEC to get the duration in seconds. It then prints the time taken. This helps track how long a section of code takes to execute.

Common Mistakes to Avoid

Mistake Why It’s a Problem
Using clock() without <time.h> Causes compiler errors
Forgetting to divide by CLOCKS_PER_SEC You’ll get results in ticks, not seconds
Using clock() to measure real-world (wall) time It only measures CPU time, not actual elapsed time
Using on multi-threaded or I/O-heavy code May not give accurate results

Limitations

  • System Dependency: The behavior and resolution of clock() can vary across different systems and compilers. Always refer to your system’s documentation for specific details.​
  • Overflow: On systems where clock_t is a 32-bit type, clock() may overflow after approximately 72 minutes if CLOCKS_PER_SEC is 1,000,000.​

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.