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

Measure Execution Time in C: time(), clock() and gettimeofday()
Fundamentals 4 min read

Measure Execution Time in C: time(), clock() and gettimeofday()

This guide demonstrates how to use the C standard library functions time(), clock(), and the POSIX gettimeofday() to measure code execution duration, comparing their precision levels and showing sample code snippets that illustrate typical usage and output.

Measuring Seconds with time()

#include <stdio.h>
#include <time.h>

int main() {
    // Get current time before operation
    time_t start_time = time(NULL);

    // Perform some operation (e.g., a simple loop)
    for (long i = 0; i < 1000000000; ++i);

    // Get current time after operation
    time_t end_time = time(NULL);

    // Compute time difference
    double duration = difftime(end_time, start_time);

    // Print time difference
    printf("Elapsed time: %.2f seconds
", duration);
    return 0;
}

Sample output:

Elapsed time: 3.00 seconds

Measuring Higher Precision with clock()

#include <stdio.h>
#include <time.h>

int main() {
    // Get CPU clock time before operation
    clock_t start_time = clock();

    // Perform some operation (e.g., a simple loop)
    for (long i = 0; i < 1000000000; ++i);

    // Get CPU clock time after operation
    clock_t end_time = clock();

    // Compute time difference
    double duration = (double)(end_time - start_time) / CLOCKS_PER_SEC;

    // Print time difference
    printf("Elapsed time: %.6f seconds
", duration);
    return 0;
}

Sample output:

Elapsed time: 2.500000 seconds

Measuring Microseconds with gettimeofday()

#include <stdio.h>
#include <sys/time.h>

int main() {
    struct timeval start, end;

    // Get current time before operation
    gettimeofday(&start, NULL);

    // Perform some operation (e.g., a simple loop)
    for (long i = 0; i < 1000000000; ++i);

    // Get current time after operation
    gettimeofday(&end, NULL);

    // Compute time difference
    double duration = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1e6;

    // Print time difference
    printf("Elapsed time: %.6f seconds
", duration);
    return 0;
}

Sample output:

Elapsed time: 2.499999 seconds

Key Points

time() : returns seconds as an integer, low precision.

clock() : provides CPU time, higher precision.

gettimeofday() : offers microsecond precision.

Actual timings vary based on system performance and the workload.

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
performance profilingC#clockgettimeofdaytime measurement

Reader feedback

Was this useful?

Useful0

Discussion 0

Questions, practical examples and thoughtful responses.

Open full discussion