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.
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.
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.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.time_once(function, data)callsfunction(data)once and returns how many seconds it took.scale(n_now, n_later, growth)returns how many times more workn_lateritems take thann_now, for agrowthof"O(1)","O(log n)","O(n)","O(n log n)"or"O(n^2)". Anything else raisesValueError.guess_growth(n, steps_n, steps_2n)returns the growth, out of those five, whosescalefromnto2 * nis nearest tosteps_2n / steps_n.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.
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
- Counting the steps
- A loop inside a loop
- What doubling does
- Five growths, side by side
- Three ways through a pile
- Loops you can't see
- One loop, or two?
- Reading the clock
- Time it twice
- From one size to a bigger one
- Three times the parts
- Which growth is it?
- How Paul would time it
- Fix Riya's version
- Make the speed tester
- Ship it