Skip to content

Checking your reading list…

Retry verification

Stay curious. Build better.

Appearance

Choose how BestHub looks on this device.

Changes apply immediately.

Search articles

Open Discover with these filters

Suggested searches

How to Measure Execution Time in C with time(), clock() and gettimeofday()
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.

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.

About this article

For questions about attribution, corrections or content rights, please contactadmin@besthub.devand we will review it promptly.

Report an issue with this article
PerformanceC#Benchmarkingclockgettimeofdaytime measurement

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion