Timing and Benchmarking Tools
Measure before you optimize.
Timing and Benchmarking Tools 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.
Measure Before You Optimize
Never guess where time goes. You first measure your code, because the slow part is rarely the part you expected. ⏱️
The time Module
Mojo ships a time module with helpers to read the clock. It is your simplest tool for timing a block of work.
from time import nowReading the Clock
The now function returns the current time in nanoseconds. Capture it just before and just after the work you want to measure.
var start = now()
# ...work...
var end = now()Computing Elapsed Time
Subtract start from end to get the elapsed nanoseconds. Divide to convert into milliseconds or seconds for readable output.
var elapsed_ns = end - start
var ms = elapsed_ns / 1_000_000Wall Clock vs Work
Wrap only the code you care about. If you also time setup or printing, that noise hides the real compute cost you are chasing.
Use the benchmark Module
For serious numbers, Mojo offers a benchmark module that runs your code many times and reports a stable average automatically.
from benchmark import keepKeep Results Alive
The keep function tells the compiler a value really matters. Without it, dead-code removal may delete the work you meant to time.
var r = compute()
keep(r)Repeat for Stable Numbers
One run is noisy from caches and the OS. Running many iterations and averaging gives a number you can actually trust.
Warm Up the Code
The first run is often slow due to cold caches and startup. A short warmup you discard makes later timings reflect steady-state speed.
Pick a Meaningful Input
Tiny inputs finish too fast to measure well. Choose an input big enough that the work takes a steady, measurable amount of time.
Time Consistently
Always benchmark on the same machine with little running in the background. Stable conditions keep one run comparable to the next.
Quick Check
Think about why we time code at all.
Recap
You learned to measure first: read now from the time module, use benchmark with keep, warm up, repeat, and time steady work. 🎯
Frequently asked questions
Is the “Timing and Benchmarking Tools” lesson free?
Yes — the full text of “Timing and Benchmarking Tools” 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 “Timing and Benchmarking Tools”?
Measure before you optimize. 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 “Timing and Benchmarking Tools” 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
- Timing and Benchmarking Tools
- Finding the Real Bottleneck
- Optimizing What Matters
- Reading a Profile Report