5.2. Profiling CPU
A profile is a set of statistics that describes how often and for how long various parts of the program executed
The profiler modules are designed to provide an execution profile for a given program, not for benchmarking purposes (for that, there is timeit for reasonably accurate results). This particularly applies to benchmarking Python code against C code: the profilers introduce overhead for Python code, but not for C-level functions, and so the C code would seem faster than any Python one.
5.2.1. Wall Time
Wall time (elapsed time)
Wall time is the time measured by a regular clock between the start and completion of an operation.
CPU execution.
Waiting for disk or network I/O.
Waiting for locks and synchronization.
Thread scheduling delays.
Sleeping and other blocking operations.
Example:
from time import time, sleep
def run():
sleep(1) # Simulate a blocking operation
return "Done"
start = time()
run()
end = time()
duration = end - start
print(f"Duration (Wall Time): {duration:.2f} seconds")
The CPU does almost nothing during those five seconds, but wall time continues increasing.
5.2.2. CPU time
CPU time (execution time)
CPU time measures the time spent executing instructions on the CPU.
CPU time is the time during which the CPU is actively executing instructions for a specific process or thread.
CPU time is a measure of the actual processing time consumed by a program, excluding time spent waiting for I/O, sleeping, or being preempted by the operating system.
CPU time is typically lower than wall time, especially for I/O-bound or multithreaded applications.
In Python, time.process_time() measures CPU time consumed by the current process,
including its threads, but excluding time spent sleeping or waiting.
from time import process_time, sleep
def run():
sleep(1) # Simulate a blocking operation
return "Done"
start = process_time()
run()
end = process_time()
duration = end - start
print(f"Duration (CPU Time): {duration:.2f} seconds")
5.2.3. Wall Time vs CPU Time
import time
wall_start = time.perf_counter()
cpu_start = time.process_time()
sum(i * i for i in range(100_000_000))
wall_elapsed = time.perf_counter() - wall_start
cpu_elapsed = time.process_time() - cpu_start
print(f"Wall time: {wall_elapsed:.3f}s")
# Wall time: 3.850s
print(f"CPU time: {cpu_elapsed:.3f}s")
# CPU time: 3.849s
| Workload | Wall time | CPU time |
|---------------------------|-----------|----------|
| CPU-intensive calculation | 5 s | 4.9 s |
| Network request | 5 s | 0.05 s |
| Sleep for 5 seconds | 5 s | ~0 s |
For multithreaded applications, CPU time can exceed wall time because multiple threads can execute simultaneously on different CPU cores.
For example:
Wall time: 5 seconds.
CPU time: 15 seconds.
Average CPU utilization: approximately 300% of one logical CPU.
Which mode should you use in Python profiling?
| Objective | Recommended mode |
|----------------------------------------------------------|------------------|
| Find functions responsible for slow application response | Wall |
| Identify computational bottlenecks | CPU |
| Investigate network or disk I/O latency | Wall |
| Investigate CPU-intensive algorithms | CPU |
| Investigate thread scheduling and blocking | Wall |
| Measure actual CPU consumption | CPU |
An important distinction. Suppose your application spends:
20% of its time calculating.
70% waiting for network responses.
10% waiting for database operations.
A CPU profiler will predominantly show the calculation.
A wall-time profiler will reveal that most of the application's latency comes from waiting.
Recommendation: Start with wall-time profiling when investigating application performance. Switch to CPU profiling when you need to understand computational efficiency.
The distinction is fundamental: optimizing CPU consumption does not necessarily improve application latency.