0Pricing
Mojo Academy · Lección

Escribir un benchmark justo

Mida la misma tarea en ambos lenguajes.

Escribir un benchmark justo 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.

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 total

Time 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() - start

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

Preguntas frecuentes

¿La lección «Escribir un benchmark justo» es gratis?

Sí — el texto completo de «Escribir un benchmark justo» 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 «Escribir un benchmark justo»?

Mida la misma tarea en ambos lenguajes. 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 «Escribir un benchmark justo»?

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. Escribir un benchmark justo
  2. Por qué fn y los tipos aceleran el código
  3. Leer los números con honestidad
  4. Hábitos idiomáticos de Mojo
← Volver a Mojo Academy