Code

The measuring harness

The tree works and the tests pass. Now two files that let you measure it: the harness and a main.go with three new commands.

Both are given complete: they are an instrument, not an exercise. You do not write them.

command what it does
algo balance step 1's table: BST and AVL, two insertion orders
algo rotate the price — build comparisons, time, and rotation count
algo reload the answer to lesson 11's first drill

What they do

  • algo balance — lesson 11's shape command run again, with the balanced tree beside the plain one. Same libraries, same two insertion orders, one extra structure. It is the table you saw in step 1.
  • algo rotate — the price: what balancing costs on input the plain BST already handled perfectly well. You read it in step 7.
  • algo reload — save through Keys() (sorted) and reload with repeated Put, as lesson 11's first drill asked.

The test file also holds four Benchmarks — they are needed in step 7, because what is measured there is something the counter cannot see:

go test -run XXX -bench 'Build|Lookup' -benchtime=20x -count=5

They need s10k.jsonl (algo gen -n 10000 -seed 7 -o s10k.jsonl); without it they are skipped.

Look at one thing

avlbench.go reads avl.Rotations — the same field rotateRight and rotateLeft increment. Where the BST row has a dash (-), the AVL row has a number. In step 7 that number explains why the counter and the clock disagree.