Writing a Fair Benchmark
Time the same task in both languages.
Writing a Fair Benchmark is a free Mojo Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Benchmark at All?
To prove Mojo is faster, you measure it. A benchmark times the exact same task in both languages so the numbers actually mean something. 📊
Same Task, Both Sides
A fair test runs identical work in Python and Mojo. If the algorithms differ, you compare ideas, not languages, and the result is meaningless.
Pick One Workload
Choose a single, clear workload, like summing a million numbers. A focused task is easy to repeat and easy to reason about.
fn sum_to(n: Int) -> Int:
var total = 0
for i in range(n):
total += i
return totalTime the Work, Not Setup
Start the clock right before the work and stop it right after. Timing file loading or imports hides the real compute cost.
from time import now
var start = now()Measure Elapsed Time
Subtract the start from the end to get elapsed time. That single number is what you will compare across the two languages.
var start = now()
# ...work...
var elapsed = now() - startRun It Many Times
One run can be noisy. Repeat the task many times and average, so a random hiccup does not decide your result.
for trial in range(10):
var t = now()
sum_to(1_000_000)Warm Up First
The first run is often slow due to caching and startup. Do a quick warmup run and ignore it before timing for real.
Use a Big Enough Input
Tiny inputs finish too fast to measure. Pick an input large enough that the work takes a meaningful, steady amount of time.
Avoid Dead-Code Removal
If a result is never used, the compiler may delete the work. Print or keep the output so the timed code actually runs.
var r = sum_to(1_000_000)
print(r)Same Machine, Same Conditions
Run both versions on the same machine with nothing heavy in the background. Different hardware ruins any comparison.
Report It Clearly
Write down the input size, number of runs, and the average time. A clear report lets anyone trust and repeat your benchmark.
Quick Check
Think about what makes a comparison trustworthy.
Recap
A fair benchmark times the same work on the same machine, warms up, repeats, and reports clearly. Now the speedup you measure is real. 🎯
Frequently asked questions
Is the “Writing a Fair Benchmark” lesson free?
Yes — the full text of “Writing a Fair Benchmark” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing a Fair Benchmark”?
Time the same task in both languages. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Mojo Academy?
No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Writing a Fair Benchmark” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Mojo Academy lesson?
Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Writing a Fair Benchmark
- Why fn and Types Speed Up
- Reading the Numbers Honestly
- Idiomatic Mojo Habits