Werkzeuge für Timing und Benchmarking
Messen Sie, bevor Sie optimieren.
Werkzeuge für Timing und Benchmarking ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „Werkzeuge für Timing und Benchmarking“ kostenlos?
Ja — der vollständige Text von „Werkzeuge für Timing und Benchmarking“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Werkzeuge für Timing und Benchmarking“?
Messen Sie, bevor Sie optimieren. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „Werkzeuge für Timing und Benchmarking“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Werkzeuge für Timing und Benchmarking
- Den echten Engpass finden
- Das Wichtige optimieren
- Einen Profiling-Bericht lesen