Paul's Python CoursePPC

Project 1 of 15, for Neil

Speed Tester

A speed tester for Neil's spares check on his kart and rocket parts: count the steps at three sizes, chart them, name the growth, time it, and say what the kart track's ten thousand parts will do.

About an hour and a half, in 17 steps.

Sign up free to open itLog in

Act 4 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • NeilThe kart track's clearing out its parts store, and all ten thousand parts are coming here!
  • NeilEvery time a part comes in, my script checks for spares by comparing every part against every other part.
  • NeilIt's fine with two hundred, but I reckon ten thousand will make it fall over. Paul just says it depends.
  • PaulIt depends on how the steps grow with the parts, and you can build something that measures exactly that.

A speed tester runs a function on bigger and bigger inputs, measures the work, and says how it grows. Build one in speed_tester.py and point it at Neil's spares check, which finds the parts his drawers hold more than one of. stock.txt lists the 200 kart and rocket parts in them today.

  1. spares_by_pairs(parts) compares every part with every part after it, as Neil's does. It returns (spares, comparisons): the names that appear more than once, in alphabetical order, each named once, and how many times two parts were compared.
  2. spares_by_set(parts) goes through the parts once, keeping a set of the ones it has seen. It returns (spares, lookups), counting each time it checks the set.
  3. time_once(function, data) calls function(data) once and returns how many seconds it took.
  4. scale(n_now, n_later, growth) returns how many times more work n_later items take than n_now, for a growth of "O(1)", "O(log n)", "O(n)", "O(n log n)" or "O(n^2)". Anything else raises ValueError.
  5. guess_growth(n, steps_n, steps_2n) returns the growth, out of those five, whose scale from n to 2 * n is nearest to steps_2n / steps_n.
  6. chart(sizes, values) returns a bar chart as one string, a line per size: the size right-aligned in six characters, |, a bar of #s padded to 40 characters, a space, and the value. The biggest value's bar is 40 long and the others are in proportion, rounded. If every value is 0, every bar is empty.

The main block is written. It charts the steps each check takes on 200, 400 and 800 different parts, times the pairs check at the same sizes, guesses each growth, and scales today's time up for the track's lot.

What it prints
Parts drawers today: 200 parts, 56 kinds with spares

By pairs, comparisons:
   200 | ##                                       19900
   400 | ##########                               79800
   800 | ######################################## 319600
Growth: O(n^2)

By set, lookups:
   200 | ##########                               200
   400 | ####################                     400
   800 | ######################################## 800
Growth: O(n)

By pairs, seconds:
   200 | ##                                       0.0006
   400 | ##########                               0.003
   800 | ######################################## 0.0125
Growth: O(n^2)

By pairs today: 0.0007 s. With the track's lot: about 1.8 s, every time a part comes in.

What you learn

What's in it

  1. Counting the steps
  2. A loop inside a loop
  3. What doubling does
  4. Five growths, side by side
  5. Three ways through a pile
  6. Loops you can't see
  7. One loop, or two?
  8. Reading the clock
  9. Time it twice
  10. From one size to a bigger one
  11. Three times the parts
  12. Which growth is it?
  13. How Paul would time it
  14. Fix Riya's version
  15. Make the speed tester
  16. Ship it