0Pricing
Mojo Academy · Lektion

Warum fn und Typen beschleunigen

Setzen Sie Striktheit mit Leistung in Beziehung.

Warum fn und Typen beschleunigen ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

Speed Comes From Knowing

Mojo is fast because it knows your types ahead of time. With fn and annotations, the compiler can generate tight machine code. ⚡

Python Decides at Runtime

Plain Python checks an object's type every time it runs an operation. That constant dispatch work is a big part of why it can feel slow.

Static Types Skip the Checks

When a type is fixed at compile time, Mojo drops the runtime type checks. The operation becomes a direct, fast instruction.

fn add(a: Int, b: Int) -> Int:
    return a + b

fn Requires Types

An fn forces you to declare types, giving the compiler everything it needs to optimize. That requirement is the source of its speed.

fn scale(x: Float64) -> Float64:
    return x * 2.0

No Boxing of Numbers

Python wraps every number in an object on the heap. A typed Mojo Int lives directly as raw bytes, with no wrapper to unpack.

var count: Int = 42

Values Fit in Registers

Because a typed number is just bytes, the CPU can keep it in a fast register instead of chasing pointers through memory.

Inlining Becomes Possible

Knowing the exact types lets the compiler inline small functions, removing call overhead in hot loops that run millions of times.

Loops Get Specialized

A typed loop compiles to a fixed sequence of CPU instructions. The compiler can even unroll or vectorize it for extra throughput.

fn total(n: Int) -> Int:
    var s = 0
    for i in range(n):
        s += i
    return s

Errors Caught Early

Types catch mismatches at compile time, so the running program spends zero effort guarding against them. Safety and speed arrive together.

def Stays Flexible

A def can skip types and behaves like Python, which is handy but slower. You choose strictness only where speed truly matters.

def greet(name):
    print("Hi", name)

Strictness Is a Trade

You trade a little flexibility for big speed. In hot paths the deal is great; fn plus types is how Mojo closes the gap with C.

Quick Check

Connect strictness to performance.

Recap

Typed fn code lets Mojo skip dynamic checks, avoid boxing, and emit specialized machine code. Strictness in hot paths is the speed secret. 🎯

Häufig gestellte Fragen

Ist die Lektion „Warum fn und Typen beschleunigen“ kostenlos?

Ja — der vollständige Text von „Warum fn und Typen beschleunigen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Warum fn und Typen beschleunigen“?

Setzen Sie Striktheit mit Leistung in Beziehung. Du übst Mojo Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Mojo Academy zu starten?

Keine Vorkenntnisse erforderlich. Mojo Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.

Wie lange dauert die Lektion „Warum fn und Typen beschleunigen“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Mojo Academy-Lektion Code schreiben und ausführen?

Ja. Jede Mojo Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einen fairen Benchmark schreiben
  2. Warum fn und Typen beschleunigen
  3. Zahlen ehrlich auswerten
  4. Idiome Mojo-Gewohnheiten
← Zurück zu Mojo Academy