題組內容
一、解釋名詞:(每小題 5 分,共 20 分)
⑶ Loop Optimization
詳解 (共 2 筆)
詳解
Loop Optimization(迴圈優化)是一種在程式設計中用於優化迴圈的技術。迴圈是一種重要的程式結構,用於執行重複的操作。迴圈的效率對於程序的性能至關重要,因此進行迴圈優化是提高程序性能的關鍵
詳解
Frequency Reduction (Code Motion):
In frequency reduction, the amount of code in loop is decreased. A statement or expression, which can be moved outside the loop body without affecting the semantics of the program, is moved outside the loop.
Example:
Initial code:
while(i<100)
{
a = Sin(x)/Cos(x) + i;
i++;
}
Optimized code:
t = Sin(x)/Cos(x);
while(i<100)
{
a = t + i;
i++;
}
Loop Unrolling:
Loop unrolling is a loop transformation technique that helps to optimize the execution time of a program. We basically remove or reduce iterations. Loop unrolling increases the program’s speed by eliminating loop control instruction and loop test instructions.
Example:
Initial code:
for (int i=0; i<5; i++)
printf(\"Pankaj\\n\");
Optimized code:
printf(\"Pankaj\\n\");
printf(\"Pankaj\\n\");
printf(\"Pankaj\\n\");
printf(\"Pankaj\\n\");
printf(\"Pankaj\\n\");
Loop Jamming:
Loop jamming is the combining the two or more loops in a single loop. It reduces the time taken to compile the many number of loops.
Example:
Initial Code:
for(int i=0; i<5; i++)
a = i + 5;
for(int i=0; i<5; i++)
b = i + 10;
Optimized code:
for(int i=0; i<5; i++)
{
a = i + 5;
b = i + 10;
}