0Pricing
Mojo Academy · レッスン

コンパイル時のループアンローリング

@parameter forでループを展開します

「コンパイル時のループアンローリング」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「コンパイル時のループアンローリング」レッスンは無料ですか?

はい。「コンパイル時のループアンローリング」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「コンパイル時のループアンローリング」で何を学びますか?

@parameter forでループを展開します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「コンパイル時のループアンローリング」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. パラメトリックアルゴリズム
  2. コンパイル時のループアンローリング
  3. 条件付きコンパイル
  4. 制約と静的チェック
← Mojo Academyに戻る