0Pricing
Mojo Academy · Lektion

Schleifen zur Compile-Zeit entrollen

Erweitern Sie Schleifen mit @parameter for.

Schleifen zur Compile-Zeit entrollen 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.

Loops the Compiler Expands

Loop unrolling means the compiler writes out each iteration as straight-line code, removing the loop's counting and branching overhead. ⚙️

The @parameter for Decorator

Put @parameter on a for loop whose range is known at compile time, and Mojo unrolls it fully instead of running it at runtime.

@parameter
for i in range(4):
    print(i)

The Bound Must Be Static

The loop range has to be a compile-time value, so the compiler knows exactly how many copies of the body to generate.

alias N = 8

@parameter
for i in range(N):
    process(i)

The Index Becomes a Constant

In an unrolled loop the index is a true parameter in each copy, so you can use it where only compile-time values are allowed.

@parameter
for i in range(4):
    arr[i] = i * i

Why Unroll at All?

Unrolling drops the per-iteration branch and counter work, giving the CPU a clean run of instructions it can pipeline efficiently.

It Pairs With SIMD

Unrolled bodies often feed SIMD operations, letting each generated copy handle one vector lane group for maximum throughput.

Indexing SIMD Lanes

A compile-time index lets you reach individual lanes of a SIMD vector, something a runtime loop variable cannot always do.

@parameter
for i in range(4):
    total += vec[i]

Bigger Code, Faster Runs

Unrolling trades a larger binary for fewer instructions executed, a worthwhile deal in tight hot loops that run millions of times.

Don't Unroll Everything

Reserve unrolling for small, hot inner loops. Unrolling huge ranges bloats code and can hurt instruction-cache performance.

A Compile-Time Tool

@parameter for is pure metaprogramming: the loop never exists at runtime because the compiler already expanded it into flat code. 🚀

Pair It With Constants

Define the bound with an alias so one named constant drives the unroll count, keeping the code clear and easy to retune later.

alias TILE = 4

@parameter
for i in range(TILE):
    work(i)

Quick Check

Recall what @parameter does to a for loop.

Recap

@parameter for unrolls statically-bounded loops at compile time: the index becomes a constant, overhead vanishes, and hot loops run faster. 🎯

Häufig gestellte Fragen

Ist die Lektion „Schleifen zur Compile-Zeit entrollen“ kostenlos?

Ja — der vollständige Text von „Schleifen zur Compile-Zeit entrollen“ 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 „Schleifen zur Compile-Zeit entrollen“?

Erweitern Sie Schleifen mit @parameter for. 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 „Schleifen zur Compile-Zeit entrollen“?

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. Parametrische Algorithmen
  2. Schleifen zur Compile-Zeit entrollen
  3. Bedingte Kompilierung
  4. Constraints und statische Prüfungen
← Zurück zu Mojo Academy