C
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Functions
-
Section 4: Arrays and Strings
-
Section 5: Pointers
-
Section 6: Memory Management
-
Section 7: Structures and Unions
-
Section 8: The Preprocessor and Build Process
-
Section 9: Standard Library: stdio.h
-
Section 10: Standard Library: stdlib.h
-
Section 11: Standard Library: string.h
-
Section 12: Standard Library: ctype.h and wctype.h
-
Section 13: Standard Library: math.h, complex.h, fenv.h, tgmath.h
-
Section 14: Standard Library: Type and Limit Headers
-
Section 15: Standard Library: Error Handling and Debugging
-
Section 16: Standard Library: Localization and Encoding
-
Section 17: Standard Library: time.h
-
Section 18: Standard Library: Concurrency (C11)
-
Section 19: POSIX and System Programming (unistd.h)
-
Section 20: More Data Structures
-
Section 21: Algorithms in C
-
Section 22: Bitwise Operations
-
Section 23: Command-Line Programs
-
Section 24: Debugging and Best Practices
-
Section 25: Compiler and Language Internals
-
Section 26: Embedded and Cross-Platform Considerations
-
Section 27: Networking Basics
-
Section 28: Practical Projects
-
Section 29: Interview Practice
-
Section 30: C23 Modern Features
-
Section 31: More Practice and Review
133: Measuring Elapsed Time with clock()
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.
There are no comments for now.