How to Measure Execution Time in C: time(), clock() and gettimeofday()
This tutorial demonstrates three C functions—time(), clock() and gettimeofday()—for measuring program execution time with second, sub‑second and microsecond precision, provides complete example code for each, and explains their accuracy differences and practical considerations.
Measuring elapsed seconds with time()
Include #include <stdio.h> and #include <time.h>. Record the start time with time_t start = time(NULL);, execute the workload, record the end time, compute the difference with difftime, and print the result.
#include <stdio.h>
#include <time.h>
int main() {
time_t start = time(NULL);
for (long i = 0; i < 1000000000L; ++i) ;
time_t end = time(NULL);
double secs = difftime(end, start);
printf("Elapsed: %.2f seconds
", secs);
return 0;
}Typical output on a modern CPU is around 3 seconds.
Higher‑resolution timing with clock()
Use clock_t start = clock(); and clock_t end = clock();. Convert the tick difference to seconds by dividing by CLOCKS_PER_SEC.
#include <stdio.h>
#include <time.h>
int main() {
clock_t start = clock();
for (long i = 0; i < 1000000000L; ++i) ;
clock_t end = clock();
double secs = (double)(end - start) / CLOCKS_PER_SEC;
printf("Elapsed: %.6f seconds
", secs);
return 0;
} CLOCKS_PER_SECis defined in time.h and its exact value varies by platform.
Microsecond‑level timing with gettimeofday()
Include #include <sys/time.h>. Declare two struct timeval variables, capture timestamps with gettimeofday(&start, NULL) and gettimeofday(&end, NULL), then compute elapsed time as seconds plus microseconds divided by 1 e6.
#include <stdio.h>
#include <sys/time.h>
int main() {
struct timeval start, end;
gettimeofday(&start, NULL);
for (long i = 0; i < 1000000000L; ++i) ;
gettimeofday(&end, NULL);
double secs = (end.tv_sec - start.tv_sec) +
(end.tv_usec - start.tv_usec) / 1e6;
printf("Elapsed: %.6f seconds
", secs);
return 0;
}Typical output shows about 2.5 seconds, demonstrating finer granularity than time() or clock().
Key considerations
time() returns whole‑second resolution; suitable only for coarse measurements.
clock() measures CPU time with higher resolution but is limited by CLOCKS_PER_SEC and may not reflect wall‑clock time on multi‑core systems.
gettimeofday() provides microsecond resolution and is appropriate for high‑precision benchmarks, though it is deprecated on some platforms in favor of clock_gettime.
Actual timings depend on hardware, compiler optimizations, and system load.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
