Scrivere un benchmark corretto
Misuri lo stesso compito in entrambi i linguaggi.
Scrivere un benchmark corretto è una lezione Mojo Academy gratuita su CoddyKit. Questa è la lezione 1 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Mojo Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Mojo Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 🎯
Domande Frequenti
La lezione «Scrivere un benchmark corretto» è gratuita?
Sì — il testo completo di «Scrivere un benchmark corretto» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Mojo Academy, passa a CoddyKit PRO. Il corso Mojo Academy include 4 lezioni in totale.
Cosa imparerò in «Scrivere un benchmark corretto»?
Misuri lo stesso compito in entrambi i linguaggi. Eserciti Mojo Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Mojo Academy?
Non è richiesta alcuna esperienza precedente. Mojo Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 1 di 4.
Quanto tempo richiede la lezione «Scrivere un benchmark corretto»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Mojo Academy?
Sì. Ogni lezione Mojo Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Scrivere un benchmark corretto
- Perché fn e i tipi aumentano la velocità
- Leggere i numeri con obiettività
- Abitudini idiomatiche in Mojo