Fast code: measure, then optimize lesson plan
Profile real code on an ESP32-C3 with micros() and the cycle counter, then make it fast: Big O, floats, the heap, tables and caches, ending in a starfield fifty times faster.
Objectives and materials
By the end, students can
- Find where the time goes before changing a line, and how it grows when the data does.
- What an instruction, a float and an allocation really cost on a 160 MHz RISC-V core.
- Work things out once and remember them: lookup tables and caches.
- Profile a slow starfield and make it fifty times faster.
Materials for the whole unit
- ESP32-C3-DevKitM-1
- Full-size breadboard
- TFT display, 240x320
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Measuring
Find where the time goes before changing a line, and how it grows when the data does.
| Lesson | Time | What students do |
|---|---|---|
| Measure firstOne frame of a space game is four jobs. Guess the slow one, then time each with micros(), learn what a measurement can and cannot see, and time a job yourself. | 50 min |
|
| Big O in practiceLinear search, binary search and a check of every pair, timed at growing sizes: watch time grow like n, like log n and like n squared, and predict the next one. | 50 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- TFT display, 240x320
Check for understanding
Each student finishes "Big O in practice" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Predict, then measure".
The machine
What an instruction, a float and an allocation really cost on a 160 MHz RISC-V core.
| Lesson | Time | What students do |
|---|---|---|
| Cycle countsRead the core's own cycle counter, see what an add, a multiply and a divide cost, find out why float math is slow on a chip with no FPU, and dim 1,000 pixels on a budget. | 50 min |
|
| Memory and allocationWhere globals, the heap and the stack live in the C3's 400 KB, what malloc really costs, why two growing Strings fight over the heap, and how reserve() ends the fight. | 50 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- TFT display, 240x320
Check for understanding
Each student finishes "Memory and allocation" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Reserve the room".
Doing less work
Work things out once and remember them: lookup tables and caches.
| Lesson | Time | What students do |
|---|---|---|
| Tables vs mathA sine wave across the panel two ways: sinf() for every column, or a table worked out once. Time both, measure the table's error in pixels, and pick its size. | 50 min |
|
| CachingRemember answers instead of working them out again: a memo table takes Fibonacci from 150,049 calls to 47, and a HUD that remembers what it drew stops redrawing every frame. | 50 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- TFT display, 240x320
Check for understanding
Each student finishes "Caching" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Draw only what changed".
Boss
Profile a slow starfield and make it fifty times faster.
| Lesson | Time | What students do |
|---|---|---|
| Boss: double a frame rateA warp-speed starfield runs at 9 frames a second. Profile it, find the job eating the frame, fix it, and push it past double, then past 300 frames a second. | 50 min |
|
Materials
- ESP32-C3-DevKitM-1
- Full-size breadboard
- TFT display, 240x320
Check for understanding
Each student finishes "Boss: double a frame rate" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Write it up".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.