Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lab 12: Performance and memory

Objectives: LO-12.1, LO-12.2, LO-12.3
Prerequisites: the Module 12 reading and Lab 11
Time: about 2 hours

You will write a benchmark helper, predict the winner of six races between two ways of doing the same thing and check your predictions, read the garbage collector’s statistics, and speed up a slow word counter at least fivefold, justifying each change with a measurement. Every number here depends on your machine and how busy it is, so the checks compare ratios, never times.

Setup

The second cell prints the Lua version, which should be Lua 5.4. Close other heavy programs before you measure: they make every timing slower and noisier.

1. A benchmark helper

Exercise L12.1 (LO-12.1, ★★, about 20 minutes)

Write bench(fn, runs), which calls fn runs times, 5 by default, after a full garbage collection before each call, times each call with os.clock, and returns two numbers: the minimum time and the median time. The median of an even number of times is the mean of the two in the middle.

2. Six races

Each race compares two ways, A and B, of doing the same work. For races 1 to 3, 5, and 6, the question is which is faster; for race 4, which uses more memory. The next cell defines the races. Read them, but don’t run the comparison yet.

Exercise L12.2 (LO-12.2, ★★, about 20 minutes)

For each race, predict the winner, "A" or "B": the faster one, or for race 4 the one that uses more memory. Write down why, from the reading. Then run the check, which measures both sides with your bench and prints the ratio. It takes about ten seconds.

3. The garbage collector’s statistics

Run the next cell. It builds and drops a hundred thousand small tables, and prints the memory in use, from collectgarbage("count"), before, after, and after a full collection. It also prints the collector’s mode, which collectgarbage("incremental") and collectgarbage("generational") set and return. The notebooks’ Lua starts in incremental mode; the standalone lua starts in generational mode.

The memory after the loop is above the start, but far below the 100,000 tables’ total size: the incremental collector ran several cycles while the loop allocated. Try it in generational mode by changing the second line.

4. Speeding up a hot path

slow_histogram(words) counts the words of a list, ignoring case, and returns one line per word, word count, most frequent first, ties in alphabetical order. The next cell defines it, and a word list of 20,000 words from a vocabulary of 2,000, and times it.

Exercise L12.3 (LO-12.3, ★★★, about 40 minutes)

Write fast_histogram(words), which returns exactly what slow_histogram returns, at least five times faster. Work by the reading’s method: time slow_histogram’s parts to find where the time goes, change one thing at a time, and measure after each change with your bench. In the Markdown cell after the check, list each change you kept, why it helps, and the time it gave.

Hint

Look for the loop inside a loop, and for the string that grows with ...

Your answer here: each change you kept, why it helps, and the time bench measured after it.

Summary

  • A benchmark repeats, collects garbage first, and reports the minimum, with the median for the spread.

  • The races’ winners followed from how Lua works: allocation in loops, copying arguments, a linear search against a table lookup, and string keys against the array part.

  • The collector reclaims temporary tables while a loop runs; its mode depends on the build.

  • The word counter’s speedup came from two algorithmic changes, not from local tricks.

Next, do the Module 12 practice.