#### Archived

This topic is now archived and is closed to further replies.

# unrolling loops?

This topic is 6006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hello all, i just have a quickie here... i hear people talking about "unrolling loops" to optimize their programs... as best as i can tell, it means something like this:
for (int i = 0; i < 1000; ++i)
SomeArray = SomeFunc(i);
...
for (int i = 0; i < 1000; ++i)
{
SomeArray[i] = SomeFunc(i);
SomeArray[++i] = SomeFunc(i);
}; 
is that what it means? if so, how much faster is this? also, will it be faster still if i "unroll" it even more?
for (int i = 0; i < 1000; ++i)
{
SomeArray[i] = SomeFunc(i);
SomeArray[++i] = SomeFunc(i);
SomeArray[++i] = SomeFunc(i);
SomeArray[++i] = SomeFunc(i);
}; 
this doesn''t really matter (at this point), i''m just curious about a term i have heard used before.
--- krez (krezisback@aol.com)

##### Share on other sites
Example of unrolling a loop:

for (iCont=0; iCont<4; iCont++)
{
myArray[iCont]=iCont
}

Unrrolling it:

myArray[0]=0
myArray[1]=1
myArray[2]=2
myArray[3]=3

Unrolling loops is usually used when the for itself takes more time than the operations inside the loop and, of course, it is being made in running time. Someone has to be insane to bother with this in an initialization code.

##### Share on other sites
It used to make a lot of sense to unroll loops (Andre LaMothe reports having unrolled a loop 25,000 times on a Commodore 64 or so). These days, however, processors cache recently fetched instructions. If you over-unroll, your loop might exceed the cache size leading to cache thrashing which will adversely affect performance.

Generally, I would say that unrolling loops is a dead optimization. Write the full application and then use a profiler to figure out where the bottlenecks are and only optimize those portions. Remember the 90/10 rule - "90% of the time is spent executing 10% of the code."

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

##### Share on other sites
I think most compilers have options to optimize for speed and or size which would take this into consideration and roll/unroll the loop apropriately.

-Marc

##### Share on other sites

for the most part, all of our render loops on the ps2
are unrolled as such...

this is all psuedo code..of course..
cause the real code is in the PS2''s VU microcode which
is seriously a pain to read..

do preliminary math for the first vertex
for (second vertex to end of list ){
do preliminary for this vertex
finish the previous vertex
}
finish the last vertex

the "preliminary" math takes a set number of ticks to get across
the pipeline before the next step in the math can actually take place, so you start a few off and by the time youve started the second or in some cases the third or fourth the first is done and ready to start the next step and so on.

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631746
• Total Posts
3002021
×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!