Paul's Python CoursePPC

Timing code

Not met yet

time.perf_counter() reads a clock made for timing code. One reading means nothing on its own: read it before, run the code, read it again, and subtract to get seconds. Timings wobble from run to run, and a job that takes a millionth of a second measures mostly the clock, so time something big enough, more than once, and trust the fastest. To see how the time grows, time the same job at two sizes and compare.

Try it

main.py

Output

Press Run and what your program prints shows up here.

Easy to get wrong

  • Thinks time.perf_counter() gives the time of day, so a single reading means something on its own.
  • Thinks one timing of a piece of code is exact and will come out the same on every run.
  • Thinks doubling the input always doubles the running time, whatever the growth.
  • Thinks timing a job that takes microseconds gives a useful measurement of it.

Where it comes up