0Pricing
Mojo Academy · Lektion

Die Funktion parallelize

Führen Sie Schleifeniterationen über Threads hinweg aus.

Die Funktion parallelize ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 1 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.

One Core Is Not Enough

A plain loop runs on a single core while the rest sit idle. For heavy work, that wastes most of your CPU.

for i in range(n):
    out[i] = heavy(i)

Spread the Work

Parallelism means handing different loop iterations to different cores so they all work at the same time. 🧵

Meet parallelize

Mojo's parallelize helper runs your loop body across many threads for you, so you skip the manual thread plumbing.

from algorithm import parallelize

The Work Closure

You write a small function that does the work for one index. Mojo calls it many times, once per iteration.

fn work(i: Int):
    out[i] = heavy(i)

Pass the Closure as a Parameter

The closure goes in square brackets as a compile-time parameter, not as a normal call argument. Mojo specializes on it.

parallelize[work](n)

The Count Is Runtime

The number in the parentheses is how many work items to run. That count is an ordinary runtime value.

parallelize[work](num_items)

Ask the Machine for Cores

Use num_physical_cores from the sys module to learn how many real cores you have to spread work across.

from sys import num_physical_cores

Cap the Worker Count

An optional second argument sets how many workers run at once. Matching it to your cores often works best.

parallelize[work](n, num_physical_cores())

Each Call Is Independent

parallelize assumes work items do not depend on each other's order. Each call should stand on its own.

Best for Heavy Loops

Parallelism shines when each item does real work. For tiny loops, the thread overhead can cost more than it saves.

Measure the Speedup

Time the loop before and after going parallel. A real benchmark tells you whether the extra threads actually helped.

Quick Check

You want to run a loop body across several CPU cores in Mojo.

Recap

You import parallelize, pass a per-item closure as a bracket parameter and the count in parentheses, then size workers to your cores for a real speedup. 🚀

Häufig gestellte Fragen

Ist die Lektion „Die Funktion parallelize“ kostenlos?

Ja — der vollständige Text von „Die Funktion parallelize“ 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 „Die Funktion parallelize“?

Führen Sie Schleifeniterationen über Threads hinweg aus. 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 1 von 4.

Wie lange dauert die Lektion „Die Funktion parallelize“?

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. Die Funktion parallelize
  2. Arbeit in Blöcke aufteilen
  3. Parallelisierung und Vektorisierung kombinieren
  4. Datenrennen vermeiden
← Zurück zu Mojo Academy