فكّ حلقات وقت الترجمة
وسّع الحلقات باستخدام @parameter for
فكّ حلقات وقت الترجمة درس مجاني في Mojo Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 * iWhy 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/7) وفتح باقي دورة Mojo Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Mojo Academy 4 دروس في المجموع.
ماذا ستتعلم في «فكّ حلقات وقت الترجمة»؟
وسّع الحلقات باستخدام @parameter for تتمرن على Mojo Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Mojo Academy؟
لا تُشترط خبرة سابقة. Mojo Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «فكّ حلقات وقت الترجمة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Mojo Academy هذا؟
نعم. كل درس في Mojo Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- الخوارزميات المعلمية
- فكّ حلقات وقت الترجمة
- الترجمة الشرطية
- القيود والفحوصات الساكنة