0Pricing
Mojo Academy · Aula

Escrevendo um teste de desempenho justo

Cronometre a mesma tarefa nas duas linguagens.

Escrevendo um teste de desempenho justo é uma aula grátis de Mojo Academy no CoddyKit. Esta é a aula 1 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Mojo Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Mojo Academy inclui 4 aulas no total.

Partes desta aula ainda não foram traduzidas e aparecem em 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. 🎯

Perguntas Frequentes

A aula “Escrevendo um teste de desempenho justo” é grátis?

Sim — o texto completo de “Escrevendo um teste de desempenho justo” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Mojo Academy, atualize para CoddyKit PRO. O curso de Mojo Academy inclui 4 aulas no total.

O que vou aprender em “Escrevendo um teste de desempenho justo”?

Cronometre a mesma tarefa nas duas linguagens. Você pratica Mojo Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.

Preciso ter experiência prévia para começar Mojo Academy?

Nenhuma experiência prévia é necessária. Mojo Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 1 de 4.

Quanto tempo leva a aula “Escrevendo um teste de desempenho justo”?

A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.

Posso escrever e executar código nesta aula de Mojo Academy?

Sim. Cada aula de Mojo Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.

Todas as aulas deste curso

  1. Escrevendo um teste de desempenho justo
  2. Por que fn e os tipos aceleram o código
  3. Lendo os números com honestidade
  4. Hábitos idiomáticos de Mojo
← Voltar para Mojo Academy