HOWTO · Python

Timer Functions in Python

This tutorial demonstrates how to monitor and analyse the running time of a python code

On this page

Use time.perf_counter() for most elapsed-duration measurements, and choose time.process_time() when you specifically want CPU time used by the current process. The time module also provides time.time() for system-clock timestamps and time.monotonic() for elapsed intervals that must not be affected by system-clock adjustments. In each timing pattern, record a start reading, run the work, record an end reading, then subtract start from end. The snippets below are illustrative; their measured durations depend on the machine and run.

Measure wall-clock time with time.time()

time.time() returns the current system-clock time in seconds since the Unix epoch, 00:00:00 UTC on January 1, 1970. Use it when you need a timestamp to store, display, or compare with another timestamp. Because it follows the system clock, an adjustment by the operating system or user can change its reading. That makes it a poor default for measuring short intervals: a clock adjustment between the two readings can distort the subtraction.

Run this example:

import time

start = time.time()
total = sum(range(1, 1001))
end = time.time()

print(f"sum={total}")
print(f"elapsed={end - start:.6f} seconds")

The first line reports the deterministic sum; the second is a measured duration and will vary. Keep the code being measured between the two readings so the subtraction covers the intended work.

Measure CPU time with time.process_time()

time.process_time() measures CPU time consumed by the current process. Its value does not include time spent sleeping or waiting for input, a network response, or another process. Use it to estimate how much processor time your code consumes, rather than how long a user waits for it to finish. Subtracting the starting reading from the ending reading gives the CPU duration for the work in between.

import time

start = time.process_time()
total = sum(number * number for number in range(100_000))
cpu_seconds = time.process_time() - start
print(f"sum={total}; CPU time={cpu_seconds:.6f} seconds")

If a task waits often, its CPU duration can be much shorter than its elapsed duration. That difference is useful: it helps distinguish computation from waiting, but it does not by itself identify what the process was waiting for.

Measure elapsed time with time.perf_counter()

time.perf_counter() is a high-resolution monotonic clock intended for measuring elapsed intervals. Its origin is unspecified, so use differences between readings rather than interpreting an individual value as a date or timestamp. Unlike process CPU time, elapsed time includes time spent sleeping or waiting. It is suitable for short functions as well as longer operations; its precision is not a reason to restrict it to small processes.

import time

start = time.perf_counter()
time.sleep(0.1)
elapsed = time.perf_counter() - start
print(f"elapsed={elapsed:.3f} seconds")

The result will be around the requested sleep duration, with additional time possible from scheduling and system load. For benchmarking, repeat comparable work and compare measurements under similar conditions rather than treating one reading as a guarantee.

Measure elapsed time with time.monotonic()

time.monotonic() also measures elapsed intervals with a clock that cannot go backward because of system-clock updates. Its reference point is undefined, so only differences between readings are meaningful. Use it for timeout deadlines or elapsed-duration checks when wall-clock changes must not affect the calculation. It does not adapt its readings to external clock changes; it avoids those changes for interval measurement.

import time

start = time.monotonic()
time.sleep(0.1)
elapsed = time.monotonic() - start
print(f"elapsed={elapsed:.3f} seconds")

Choose time.time() when you need a Unix timestamp, time.process_time() for CPU consumed by the process, and time.perf_counter() for general elapsed-time measurements. Choose time.monotonic() when an interval or timeout must remain reliable across system-clock changes. In all cases, the code between the two readings defines what the measured duration covers.