time.perf_counter
A Python function that returns a high-resolution clock reading in seconds, used to time short pieces of code.
In more detail
Call it before and after the code you want to measure and subtract the two values. Only the difference is meaningful; the starting point of the clock is undefined. For reliable numbers, repeat the operation many times in a loop and time the whole loop.
Example
t0 = time.perf_counter(); work(); elapsed = time.perf_counter() - t0Related terms
- benchmark — A repeatable measurement of how long code takes, or how much it uses, under a fixed workload, or more broadly a standard point of comparison such as an industry average.
- time complexity — A description of how the amount of work an algorithm does grows as its input gets bigger.
Taught in
- Why Complexity Matters In Production — Algorithms & Data Structures for Working Engineers
- Arrays: Contiguous Memory And Constant-Time Access — Algorithms & Data Structures for Working Engineers
- Counting And Radix Sort: Beating N Log N — Algorithms & Data Structures for Working Engineers