Loop Unrolling Loop unrolling

We mentioned loop unrolling (loop unrolling) in the fifth chapter caspp 5.2 in. Here to expand why loop unrolling can improve the efficiency of the program.

In the book arrays and calculate an example of the two codes:

1. undeployed:

 void psum1(float a[], float p[], long int n)
 {
 long int i;
 p[0] = a[0];
 for (i = 1; i < n; i++)
 p[i] = p[i-1] + a[i];
 }

2. Expand:

 void psum2(float a[], float p[], long int n)
 {
 long int i;
 p[0] = a[0];
 for (i = 1; i < n-1; i+=2) {
 float mid_val = p[i-1] + a[i];
 p[i] = mid_val;
 p[i+1] = mid_val + a[i+1];
 }
 /* For odd n, finish remaining element */
 if (i < n)
 p[i] = p[i-1] + a[i];
 }

More than two pieces of code are functionally equivalent (calculated and arrays), but after expansion of the code up and running faster. The point is that the expanded code that the compiler needs to perform an instruction is reduced.

When the cycle is not expanded, using a for loop to iterate through the array, each execution of a superposition operation - which means that the array how long, how many times compiler operations to determine the conditions for cycling will be executed .

Guess you like

Origin www.cnblogs.com/liez/p/11621282.html