How it works
Fast code: measure, then optimize is an advanced Coding course in seven lessons of C++. Every lesson runs on the same circuit: an ESP32-C3 driving a 2.4 inch ILI9341 panel over four wires (SCK 20, MOSI 21, CS 18, DC 19), the first stage of the handheld console. Each sketch times its own code and prints the numbers, and every number a lesson quotes is one the board printed.
The edit steps set a budget. The sketch prints a "budget met" line only when your change has really made the code fast enough, so the check proves the speedup, not the typing.
The lessons
- Measure first: four jobs in one frame of a space game, timed with
micros(). The HUD takes 5,210 us and the math 6, so the guess most people make is wrong. Then the limits of the ruler, averaging 100 runs, timing a job yourself, and Amdahl's law. - Big O in practice: linear search grows 15.9 times when the catalog grows 16 times, binary search 1.5 times, and checking every pair of stars quadruples each time the stars double. Predict 400 stars, then measure it (and meet the birthday problem).
- Cycle counts: the core's cycle counter, 160 cycles a microsecond, an int add at 4 cycles against a float add at 88 on a chip with no FPU, and dimming 1,000 pixels forty times faster with a multiply and a shift.
- Memory and allocation: where globals, the heap and the stack live,
1,168 cycles for one malloc and free, two Strings fighting over the heap,
and
reserve()to stop the moving. - Tables vs math: a sine wave from
sinf()against one from a table, 176 times faster, with the error measured in pixels and the table sized to keep the wave within 4 pixels. - Caching: Fibonacci from 150,049 calls to 47 with a memo table, and a HUD that remembers what it drew, 7 redraws instead of 60.
- Boss: double a frame rate: the finale.
The catalog lists an ESP32-C3 and an ILI9341, and the course uses exactly those.
The finale
A warp-speed starfield clears the whole screen every frame and runs at 9 frames a second. Its profile shows the clear taking 109 ms of a 114 ms frame. Erasing only each star's old square takes it to 174 frames a second, and drawing the HUD only when its number changes takes it to over 440: fifty times the frame rate, the same picture, about ten lines changed. The lesson ends with a measure, change, measure table you can share.
What the C3 engine does differently
- One instruction is one cycle. The engine runs the core at exactly 160 MHz with no cache, flash waits or stalls, so the cycle counter is also an instruction count. On a real C3 an integer divide costs several cycles and code from flash waits on a cache; the float costs hold, because soft float is the same instructions there.
- The cycle counter is
rdcycle. The engine has the standard RISC-V counter. A real ESP32-C3 keeps its count in the vendor register mpccr (0x7E2), which ESP-IDF'sesp_cpu_get_cycle_count()reads. Lesson 3 says which line to swap. - The panel is bit-banged. SPI is not modeled, so every pixel is sixteen pin changes from software, and a full screen takes about 110 ms. A real C3 would use its hardware SPI and draw far faster, and the same fixes would still matter.
- The counter stops while the core sleeps.
delay()parks the core, so time a delay withmicros(), not cycles. - Numbers move a little with the compiler. Run uses the prebuilt sketch; an edited sketch is built in your browser, and its timings can differ by a few percent. The budgets leave room for that.