Eine Schleife vektorisieren
Machen Sie aus einer Skalar- eine Vektorverarbeitung.
Eine Schleife vektorisieren ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 4 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.
The Scalar Loop
A plain loop touches one element per step. It is clear but leaves your CPU's vector lanes mostly unused.
for i in range(n):
out[i] = a[i] + b[i]The Vector Idea
Vectorizing means each loop step handles a whole pack of elements instead of one. You do more work per iteration.
Load a Chunk
Instead of one value, you load several at once into a SIMD vector. That single load grabs a slice of your data.
var chunk = a.load[width=4](i)Compute the Whole Chunk
With both chunks in SIMD form, add them in one element-wise step. The math covers every lane at once.
var sum = a.load[width=4](i) + b.load[width=4](i)Store the Result
Write the whole SIMD result back in one move with a vector store. One write covers all the lanes you computed.
out.store[width=4](i, sum)Step by the Width
A vectorized loop advances by the width, not by one. Each pass covers a full pack of elements.
for i in range(0, n, 4):
passLet Mojo Drive It
Mojo's vectorize helper runs a closure across the range in vector steps and handles the bookkeeping for you.
from algorithm import vectorizeA vectorize Closure
You write a small parameterized function that processes one chunk. Mojo calls it with the right width as it sweeps the range.
fn work[w: Int](i: Int):
out.store[width=w](i, a.load[width=w](i) + b.load[width=w](i))Run the Vectorize
Call vectorize with your chunk function, the width, and the total size. It does the looping and the tail for you.
vectorize[work, 4](n)Same Result, Far Faster
The vectorized loop gives identical output to the scalar one but moves through data in big steps, so it finishes sooner.
Measure to Confirm
Always time both versions. Vectorization usually wins big, but a benchmark proves the gain on your real data.
Quick Check
You replace a one-element-per-step loop with SIMD loads and stores. How should the loop advance?
Recap
Vectorizing loads and stores whole chunks, steps by the width, and lets vectorize handle the sweep and tail, giving the same result far faster. 🚀
Häufig gestellte Fragen
Ist die Lektion „Eine Schleife vektorisieren“ kostenlos?
Ja — der vollständige Text von „Eine Schleife vektorisieren“ 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 „Eine Schleife vektorisieren“?
Machen Sie aus einer Skalar- eine Vektorverarbeitung. 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 4 von 4.
Wie lange dauert die Lektion „Eine Schleife vektorisieren“?
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
- Der SIMD-Typ
- Elementweise SIMD-Mathematik
- Die passende SIMD-Breite wählen
- Eine Schleife vektorisieren