Skip to Content
Course content

133: Measuring Elapsed Time with clock()

Click on the "Edit" button in the top corner of the screen to edit your slide content.

How do I actually use clock() to see how long a piece of code takes?

The basic pattern is pretty straightforward: you take a "snapshot" of the processor time right before your heavy lifting starts, and another one immediately after it finishes. The difference between those two snapshots is the time spent.

You'll need the <time.h> header and the clock_t type, which is essentially just an integer type designed to hold clock ticks. Here is a quick example where I'm measuring how long it takes to sum a large array of integers:

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

int main() {
    clock_t start, end;
    double cpu_time_used;
    int sum = 0;

    start = clock(); // Snapshot 1

    for (long i = 0; i < 100000000; i++) {
        sum += i;
    }

    end = clock(); // Snapshot 2

    cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;
    printf("Sum: %d\n", sum);
    printf("Elapsed CPU time: %f seconds\n", cpu_time_used);

    return 0;
}

Why am I getting massive numbers instead of seconds?

If you just subtract start from end and print the result, you aren't looking at seconds; you're looking at "clock ticks." The number of ticks per second varies depending on the system you're running on, which is why C provides the constant CLOCKS_PER_SEC.

I see a lot of people make the mistake of doing integer division here. If you do (end - start) / CLOCKS_PER_SEC without casting, you'll likely get 0 for any operation that takes less than a full second. Always cast your difference to a double first so you can see those fractional seconds.

Does this measure "wall-clock" time or something else?

This is a crucial distinction: clock() measures CPU time, not real-world elapsed time. In other words, it tracks how much time the processor actually spent executing your program's instructions.

If your program calls sleep() for five seconds, clock() won't count that time because the CPU wasn't actually working on your process; it was just waiting. On the flip side, if you're running a multi-threaded program on a quad-core machine, clock() might actually return a value higher than the actual wall-clock time because it sums the time spent across all cores. If you need to know exactly when a user clicked a button and when the result appeared on the screen, clock() is the wrong tool—you'd want something like time() or platform-specific APIs. But for benchmarking an algorithm? clock() is exactly what you want.




📋 Practical Task

Benchmarking Linear vs. Binary Search

Write a program that generates a sorted array of 100,000 integers. Implement both a Linear Search and a Binary Search to find a specific number located near the end of the array.

Use clock() to measure the execution time of both search functions. Your program should print the time taken by each search in seconds. Since Binary Search is incredibly fast, you might find the result is 0.000000; if that happens, try running the search function inside a loop 1,000 times and then divide the total elapsed time by 1,000 to get a more accurate average.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.