Source: https://mokxi.com/teachers/plans/performance-and-profiling
Updated: 2026-11-19

Lesson plan

# 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.

4 chapters, 7 lessons, about 6 hours, on the ESP32-C3-DevKitM-1. Every lesson runs in the browser, with nothing to install.

Start a class from this course

The unit

## 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.

Chapter 1. About 1 h 40 min

## Measuring

Find where the time goes before changing a line, and how it grows when the data does.

Lesson | Time | What students do

Measure first One 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

- Guess, then run
- How micros() measures
- One run can lie
- Time the stars yourself
- Speed up the biggest number

Big O in practice Linear 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

- Linear search grows with n
- Binary search barely notices
- The growth, side by side
- Every pair is n squared
- Predict, then measure

### 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".

Chapter 2. About 1 h 40 min

## 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 counts Read 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

- A finer ruler
- What measuring costs
- Whole numbers against floats
- Divides and square roots
- Dim 1,000 pixels on a budget

Memory and allocation Where 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

- Three kinds of memory
- How much is left
- What malloc costs
- Two Strings fight over the heap
- Reserve the room

### 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".

Chapter 3. About 1 h 40 min

## Doing less work

Work things out once and remember them: lookup tables and caches.

Lesson | Time | What students do

Tables vs math A 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

- Two waves
- Work it out once
- What the table gets wrong
- What the table costs
- Pick the size

Caching Remember 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

- The same answer, again and again
- Count the calls
- Remember each answer
- The screen is a cache too
- Draw only what changed

### 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".

Chapter 4. About 50 min

## Boss

Profile a slow starfield and make it fifty times faster.

Lesson | Time | What students do

Boss: double a frame rate A 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

- Nine frames a second
- Read the profile
- Erase only what moved
- Measure again, fix the next one
- Write it up

### 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".

Your class

## 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.

Start a class from this course See the demo class

More units

## Other lesson plans

Electronics basics 20 lessons, about 10 h. Embedded on the Arduino Uno 16 lessons, about 12 h. Embedded on the ESP32-C3 13 lessons, about 11 h. Embedded on the Raspberry Pi Pico 7 lessons, about 6 h. Build your own fight stick 5 lessons, about 4 h. Embedded on the STM32F411 6 lessons, about 6 h. C++ on a board 13 lessons, about 10 h. Digital logic and memory 4 lessons, about 4 h. Interactive devices on the Uno 9 lessons, about 8 h. From gates to a computer 23 lessons, about 16 h. Build a claw machine 8 lessons, about 6 h. Every unit All the courses as ready-made units, on the teachers page.
