Fundamentals 4 min read

How to Measure Execution Time in C with time(), clock() and gettimeofday()

This guide shows how to benchmark C code by measuring elapsed time using the standard time() function for second‑level precision, clock() for higher CPU‑time accuracy, and gettimeofday() for microsecond‑level resolution, including complete example programs and key considerations.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Measure Execution Time in C with time(), clock() and gettimeofday()

Measuring elapsed time with time()

The time() function returns the current calendar time as a time_t value (seconds since the Unix epoch). Record the value before and after the code segment and compute the difference with difftime to obtain the elapsed time in whole seconds.

#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 duration = difftime(end, start);
    printf("Elapsed time: %.2f seconds
", duration);
    return 0;
}

Higher‑resolution timing with clock()

clock()

returns the processor time consumed by the program as a clock_t. Dividing the difference by the macro CLOCKS_PER_SEC converts the value to seconds, typically giving millisecond‑level precision.

#include <stdio.h>
#include <time.h>
int main() {
    clock_t start = clock();
    for (long i = 0; i < 1000000000L; ++i);
    clock_t end = clock();
    double duration = (double)(end - start) / CLOCKS_PER_SEC;
    printf("Elapsed time: %.6f seconds
", duration);
    return 0;
}

Microsecond precision using gettimeofday()

The POSIX gettimeofday() function fills a struct timeval with seconds and microseconds since the epoch. By subtracting the start and end structures you can achieve microsecond‑level timing.

#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 duration = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1e6;
    printf("Elapsed time: %.6f seconds
", duration);
    return 0;
}

Key considerations

time() : returns whole‑second granularity; low precision but simple for coarse measurements.

clock() : measures CPU time; precision depends on CLOCKS_PER_SEC and the operating system.

gettimeofday() : provides wall‑clock time with microsecond resolution; may be affected by system load or clock adjustments (e.g., NTP).

Actual timing results will vary with hardware, compiler optimizations, and OS scheduling; the numbers shown in the examples are illustrative.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceCBenchmarkingclockgettimeofdaytime measurement
Liangxu Linux
Written by

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.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.