Elapsed time measures wall-clock duration including waiting; process CPU time counts execution time consumed by the process.
Understanding the Concept
Elapsed time measures wall-clock duration including waiting; process CPU time counts execution time consumed by the process. Compare both to identify I/O waits.
Clocks serve different purposes; neither single sample describes overall throughput.
How to interpret the measurement
Before optimizing, establish an objective measurement and a representative workload. Memory size, function-call counts, elapsed time and samples are different metrics: knowing one does not determine the others. Repeat observations before drawing a conclusion about a performance regression.
Profiling output is evidence for finding a bottleneck, not a performance guarantee. Check that the program still produces the correct result after any optimization and note version, input size and measurement conditions.
Worked Example
Read the code from the first line to the last. Identify which values are inputs, which operation is being demonstrated and what should happen before running it.
import time
start_wall = time.perf_counter()
start_cpu = time.process_time()
time.sleep(0.01)
print(time.perf_counter() - start_wall >= 0.01)
print(time.process_time() - start_cpu >= 0)
Walkthrough: What Each Part Does
- The imports make time available. These are standard-library building blocks unless the example explicitly names an external tool.
- The printed number or profile is diagnostic data. Repeat the observation and identify the unit before comparing values.
This demonstration focuses on one practical behavior. Before changing several lines, vary one input or one configuration choice and check whether the result changes for the reason you expected.
Example Output
True
True
The output above was captured by running the example in the available Python environment. The order of concurrent events, durations and platform-dependent values can legitimately differ on your computer.
Important Behavior and Edge Cases
Assuming process_time includes time spent blocked on sleep or network I/O.
Clocks serve different purposes; neither single sample describes overall throughput.
Practice Exercise
Measure a CPU calculation and a short sleep separately, reporting the two clock differences.
Write down the result you expect before running your changed code. Check at least one normal case and one boundary or failure case. If your example starts workers or opens resources, verify how they are cleaned up.
Further reading: official documentation and API reference.
Common Beginner Mistakes
- Assuming process_time includes time spent blocked on sleep or network I/O.
- Comparing results from different workloads or environments as if the measurements were directly interchangeable.
Key Takeaways
- Elapsed time measures wall-clock duration including waiting; process CPU time counts execution time consumed by the process.
- Clocks serve different purposes; neither single sample describes overall throughput.
- Practice: Measure a CPU calculation and a short sleep separately, reporting the two clock differences.