0Pricing
Lua Academy · Lesson

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

  1. LuaJIT Architecture Overview
  2. Writing JIT-Friendly Lua
  3. Profiling with jit.p and perf
  4. Benchmarking Patterns
← Back to Lua Academy