Measuring program execution time in C helps you benchmark algorithms and quantify how long different approaches take. The standard tool is the clock() function from <time.h>, which measures CPU time consumed by the program — not wall-clock time. Two snapshots (before and after) divided by CLOCKS_PER_SEC give you the elapsed time in seconds.
clock() and CLOCKS_PER_SEC
| Concept | Explanation |
|---|---|
clock_t start = clock() |
Records CPU ticks at the start |
clock_t end = clock() |
Records CPU ticks at the end |
(end - start) |
Number of CPU ticks elapsed |
CLOCKS_PER_SEC |
Implementation-defined ticks per second (typically 1,000,000 on Linux, 1,000 on Windows) |
(double)(end-start) / CLOCKS_PER_SEC |
Elapsed time in seconds |
C Program to Measure Execution Time
/* Measure program execution time using clock() from time.h
* Compile: gcc -ansi -Wall -Wextra exectime.c -o exectime */
#include <stdio.h>
#include <time.h>
int main(void)
{
clock_t start, end;
double elapsed;
long i;
long sum = 0;
start = clock();
/* do some measurable work: sum 1 to 100 million */
for (i = 1; i <= 100000000L; i++)
sum += i;
end = clock();
elapsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Sum 1 to 100,000,000 = %ld\n", sum);
printf("Execution time = %.4f seconds\n", elapsed);
printf("CLOCKS_PER_SEC = %ld\n", (long)CLOCKS_PER_SEC);
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra exectime.c -o exectime
./exectime
Sample Output
Sum 1 to 100,000,000 = 5000000050000000 Execution time = 0.3942 seconds CLOCKS_PER_SEC = 1000000
How to Time Any Block of Code
Place clock() calls around exactly the code you want to measure. Putting the call after variable declarations (but before the work) avoids timing I/O or setup:
clock_t start, end;
double elapsed;
/* setup code — not timed */
start = clock();
/* — work to time — */
end = clock();
elapsed = (double)(end - start) / CLOCKS_PER_SEC;
printf("Elapsed: %.6f seconds\n", elapsed);
Code Explanation
clock_t— an arithmetic type defined in<time.h>. On most systems it is alongorlong long. It counts implementation-defined “clock ticks” since program start.CLOCKS_PER_SEC— a compile-time constant. On Linux it is typically 1,000,000 (microsecond resolution). On Windows it is typically 1,000 (millisecond resolution). Dividing by this constant converts ticks to seconds.- Cast to
doublebefore dividing —(double)(end - start)performs integer arithmetic first, then converts to double. Without the cast, integer division would truncate (e.g., 394,200 / 1,000,000 = 0). The cast gives 0.3942 seconds. 100000000L— the L suffix — forces the constant to belong. Without it, 100000000 might overflow a 16-bit int on small embedded targets (though on 32-bit Linux int is 32 bits and handles it).- CPU time vs wall clock time —
clock()measures CPU time used by the program. If the process sleeps or waits for I/O, that time is NOT counted. For wall-clock time, usegettimeofday()(POSIX) ortimespec_get()(C11). Useclock()to measure algorithmic efficiency.
clock() vs Other Timing Methods
| Function | Header | What it measures | Resolution | Portability |
|---|---|---|---|---|
clock() |
time.h |
CPU time (not sleeping) | 1/CLOCKS_PER_SEC | C89 — all platforms |
time() |
time.h |
Wall clock (seconds) | 1 second | C89 — all platforms |
gettimeofday() |
sys/time.h |
Wall clock (microseconds) | 1 µs | POSIX only |
clock_gettime() |
time.h |
Wall or CPU (nanoseconds) | 1 ns | POSIX / C11 |
What This Program Teaches
- Profiling loops with clock() — wrapping a computation between two
clock()calls is the portable C way to benchmark. It tells you how many CPU seconds the code consumed, independent of CPU frequency. - Avoiding integer division — always cast to
doublebefore dividing by CLOCKS_PER_SEC. This is one of the most common precision bugs in C timing code. - Gauss sum verification — the expected result of summing 1 to N is N×(N+1)/2. For N=100,000,000: 100,000,000 × 100,000,001 / 2 = 5,000,000,050,000,000. The program output matches, confirming the loop ran correctly.
- CPU time vs wall time — CPU time and wall time differ on multi-core machines (where a parallel program can use more CPU than wall seconds), on systems under load, and whenever the process blocks on I/O or sleep. clock() always measures the single-thread CPU cost.
Related Programs
- sizeof Data Types in C
- All Data Types in C
- Command Line Arguments in C
- Binomial Coefficients in C
- Bubble Sort in C
Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
|
C Programming: A Modern Approach — K.N. King (India) |
(US)
Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.