Empirical timings with perf_counter

Beginner Asymptotics & empirical timing
Created by Pavel · 29.04.2026 at 19:08 UTC

time.perf_counter() returns a float timestamp from a monotonic clock suitable for deltas: subtract end from start to measure elapsed seconds. It is what you want for micro-benchmarks of NumPy calls, sklearn fit, or small pure-Python kernels—not time.time() (NTP adjustments can move backwards) and usually not time.process_time() alone if you care about wall-clock waits.

Empirical measurement is noisy: cold caches, JIT warm-up, other processes, and Python’s allocator all jitter results. Discard the first iteration, repeat, and report median or mean over several runs when comparing approaches.

For coursework, pairing asymptotic reasoning with a few timed curves (doubling n) builds intuition that Big-O alone cannot supply.

time.perf_counter docs: [1].


Sources

University approvals: 0
Tasks
Question 1

time.perf_counter() is primarily intended for:

Hint

Subtract two readings.

Question 2

You time a function twice: first call after process start, second call after 1000 identical warm-up calls. The first call is often slower mainly because:

Hint

First execution pays setup costs.

Question 3

Implement timing_ms(run_once) that calls run_once() once and returns elapsed wall time in milliseconds as a float, using time.perf_counter().

Hint

Delta in seconds, then multiply by 1000.

Starter code is prefilled; replace TODO blocks with your solution.
2 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Card Info
  • Topic: Asymptotics & empirical timing
  • Difficulty: Beginner
  • Completed: 0 users
Creator
Pavel
Pavel