0Pricing
Mojo Academy · Lezione

Strumenti per misurazione e benchmark

Misuri prima di ottimizzare.

Strumenti per misurazione e benchmark è 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.

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 now

Reading 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_000

Wall 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 keep

Keep 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. 🎯

Domande Frequenti

La lezione «Strumenti per misurazione e benchmark» è gratuita?

Sì — il testo completo di «Strumenti per misurazione e benchmark» è 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 «Strumenti per misurazione e benchmark»?

Misuri prima di ottimizzare. 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 «Strumenti per misurazione e benchmark»?

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

  1. Strumenti per misurazione e benchmark
  2. Trovare il vero collo di bottiglia
  3. Ottimizzare ciò che conta
  4. Leggere un report di profilazione
← Torna a Mojo Academy