Skip to main content

How to Measure How Long Python Code Takes with time.perf_counter

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Before you make code faster, find out how long it takes. time.perf_counter() reads a clock made for measuring short durations: read it before the code, read it again after, and the difference is how long the code took. This guide times a sum of ten million numbers on Python 3.15.2.

Syntax​

Syntax
start = time.perf_counter()
<code to time>
elapsed = time.perf_counter() - start
PartWhat it means
time.perf_counter()Reads the clock and returns a number of seconds, as a float. It needs import time first
<code to time>The code you want to measure
elapsedThe second reading minus the first: how many seconds the code took

The Python documentation describes it: "Return the value (in fractional seconds) of a performance counter, i.e. a clock with the highest available resolution to measure a short duration." And: "The reference point of the returned value is undefined, so that only the difference between the results of two calls is valid."

The Example​

A script adds up the numbers from 0 to 9,999,999 with sum(range(10_000_000)), times it, and prints the total and the time.

Prerequisites​

  • uv and Python 3. This guide uses uv 0.13.10 with Python 3.15.2, in a project made with uv init.

Step 1: Write the Script​

timer.py
import time

start = time.perf_counter()
total = sum(range(10_000_000))
elapsed = time.perf_counter() - start

print(total)
print(f"That took {elapsed:.3f} seconds")

The timer.py script in a macOS-style code window: it reads time.perf_counter() into start, adds up range(10_000_000), subtracts start from a second reading to get elapsed, and prints the total and the time to three decimal places.

start holds the first reading, taken just before the sum. The second reading comes straight after it, and subtracting start leaves the seconds in between. In the f-string, :.3f shows the number with three decimal places.

Step 2: Run It​

Terminal
uv run python timer.py
Output
49999995000000
That took 0.084 seconds

The terminal after uv run python timer.py: the total 49999995000000 and That took 0.084 seconds.

The total is 49,999,995,000,000, and on this M1 Pro adding it up took 0.084 seconds: 84 thousandths of a second. The command works unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Printing one reading on its ownprint(time.perf_counter()) printed 31701.091397958: seconds counted from an undefined starting point, which mean nothing aloneSubtract one reading from another
Leaving off the parentheses: start = time.perf_counterstart held the function itself, not a reading, so the subtraction failed: TypeError: unsupported operand type(s) for -: 'builtin_function_or_method' and 'builtin_function_or_method'Call it: time.perf_counter()

FAQ​

Why not use time.time()?​

time.time() reads the computer's clock, which can be changed. Its documentation warns that it "can return a lower value than a previous call if the system clock has been set back between the two calls." On CPython, perf_counter() "is a monotonic clock, i.e. a clock that cannot go backwards."

How do I get whole nanoseconds?​

Use time.perf_counter_ns(). It works the same way, but returns a whole number of nanoseconds, billionths of a second:

nanoseconds.py
import time

start = time.perf_counter_ns()
total = sum(range(10_000_000))
elapsed = time.perf_counter_ns() - start
print(elapsed)
Output
84757708

84,757,708 nanoseconds is about 0.085 seconds. The documentation recommends it "to avoid the precision loss caused by the float type."

Conclusion​

time.perf_counter() read before and after sum(range(10_000_000)) showed that the sum took 0.084 seconds. A single reading means nothing; the difference between two is the time.

Additional Resources​