0Pricing
Mojo Academy · Lección

Herramientas de medición y benchmarking

Mida antes de optimizar.

Herramientas de medición y benchmarking es una lección gratuita de Mojo Academy en CoddyKit. Esta es la lección 1 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Mojo Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Mojo Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en inglés.

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

Preguntas frecuentes

¿La lección «Herramientas de medición y benchmarking» es gratis?

Sí — el texto completo de «Herramientas de medición y benchmarking» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Mojo Academy, actualiza a CoddyKit PRO. El curso de Mojo Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Herramientas de medición y benchmarking»?

Mida antes de optimizar. Practicas Mojo Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Mojo Academy?

No se requiere experiencia previa. Mojo Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 1 de 4.

¿Cuánto tiempo toma la lección «Herramientas de medición y benchmarking»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Mojo Academy?

Sí. Cada lección de Mojo Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Herramientas de medición y benchmarking
  2. Encontrar el cuello de botella real
  3. Optimizar lo que importa
  4. Leer un informe de perfilado
← Volver a Mojo Academy