While much of the time, the entity that performs the loop-unrolling will be an optimizing compiler, you the programmer can also do the task in your high-level language. Taking the loop

for(int i=0;i<1000;i++)
a[i] = b[i] + c[i];

You can quickly and simply halve the number of iterations by changing it to this, instead:

for(int i=0;i<1000;i+=2) {
a[i] = b[i] + c[i];
a[i+1] = b[i+1] + c[i+1];
}

Note, however, that with a stupid compiler (one that does not optimize well), the second fragment may be worse than the first, because of the extra addtions needed in each loop (i+1). In theory, both loops would be unrolled and rescheduled by the compiler to exactly the same assembly code. Be aware of outsmarting the compiler's optimizing techniques; the folk who write compilers today are very good at what they do.


Prev Next