0Pricing
Mojo Academy · บทเรียน

ฟังก์ชัน parallelize

เรียกใช้รอบของลูปข้ามเธรด

ฟังก์ชัน parallelize เป็นบทเรียน Mojo Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Mojo Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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

คำถามที่พบบ่อย

บทเรียน “ฟังก์ชัน parallelize” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ฟังก์ชัน parallelize” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Mojo Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Mojo Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ฟังก์ชัน parallelize”

เรียกใช้รอบของลูปข้ามเธรด คุณปฏิบัติ Mojo Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Mojo Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Mojo Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ฟังก์ชัน parallelize” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Mojo Academy นี้ได้ไหม

ได้ บทเรียน Mojo Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ฟังก์ชัน parallelize
  2. การแบ่งงานเป็นชิ้น
  3. การผสานการทำงานแบบขนานกับเวกเตอร์
  4. การหลีกเลี่ยงภาวะแข่งขันข้อมูล
← กลับไปที่ Mojo Academy