Master the fundamental concepts of build a bytecode vm through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksYou cannot optimize what you do not measure. Comparing your interpreter against CPython on the same workload reveals whether dispatch, allocation, or call overhead dominates.
Sound micro-benchmarks follow a few rules:
Dispatch styles matter:
For example, `fib(35)` in bytecode versus the same algorithm in native C often shows a 50x or larger gap, mostly from per-opcode dispatch.
Use `clock()` from `<time.h>` and report milliseconds per iteration.
Report at least median and spread, not a single timing sample. OS jitter and cache warmth dominate short runs; three warmup iterations plus five measured runs is a minimal credible methodology for comparing interpreter variants.
This exercise asks you to benchmark recursive Fibonacci in your VM against a C baseline. You will implement warmup runs, timed iterations, and printed comparison so you know where cycles go before optimizing.
"Benchmark your VM against CPython" really means understanding where an interpreter spends its dispatches. Wall-clock times differ on every machine, so profile the VM exactly instead: run two fixed Fibonacci programs on a small stack VM with 64-bit values, count every dispatched instruction per opcode, and measure how the recursive version's cost grows. Each step multiplies it by about 1.618, the golden ratio. The iterative version's cost grows linearly. Then convert dispatches into an estimated time.
cLoading…
Recursive, argument in local 0 of the main frame:
cLoading…
Iterative, n in local 0, with a in local 1 and b in local 2:
cLoading…
a < b. JZ pops and jumps if the value is 0.cLoading…
ms with 3 truncated decimals.recursive: n must be 0..30, iterative: n must be 0..90, and dispatch: 1..1000 ns.Input:
cLoading…
Output:
cLoading…
Hidden tests cover fib(0) and fib(1), consecutive recursive runs at larger n, the iterative version at n = 90 (the result needs 64 bits), a custom dispatch cost, and out-of-range arguments.