Benchmarking Patterns
Write reliable micro-benchmarks using os.clock and avoid common pitfalls.
Why Benchmark?
Micro-benchmarks measure the performance of a specific code snippet in isolation. They help verify that an optimization actually improves speed without relying on intuition.
Basic Benchmark Template
Wrap the code in a loop, measure wall time with os.clock(), and divide by iteration count.
local N = 1e6
local t0 = os.clock()
for i = 1, N do
-- code to benchmark
end
local elapsed = os.clock() - t0
print(("%.2f ns/op"):format(elapsed / N * 1e9))All lessons in this course
- LuaJIT Architecture Overview
- Writing JIT-Friendly Lua
- Profiling with jit.p and perf
- Benchmarking Patterns