Server Help

Trash Talk - So, you wanna optomize your loop, huh?

CypherJF - Fri Apr 01, 2005 6:40 pm
Post subject: So, you wanna optomize your loop, huh?
An interesting article:

http://www-128.ibm.com/developerworks/power/library/pa-unrollav3/

I haven't had time to read it completely
Mr Ekted - Fri Apr 01, 2005 7:34 pm
Post subject:
Classic example (if you care about speed):

Code: Show/Hide
// bad code

for (i = 0; i < 1000000; i++)
   {
   if (something)
      Func1(i);
   else
      Func2(i);
   }

// good code

if (something)
   for (i = 0; i < 1000000; i++)
      Func1(i);
else
   for (i = 0; i < 1000000; i++)
      Func2(i);


Of course this is a hypothetical case. You would more likely embed the loops in the functions instead of calling them a million times.
Cyan~Fire - Fri Apr 01, 2005 11:55 pm
Post subject:
That article wasn't really about optimizing loops, it was optimizing loops with AltiVec or whatever.

What Ekted said applies far more to the majority of us. (Those of us who don't use PowerPCs?) icon_razz.gif
Dr Brain - Sat Apr 02, 2005 12:15 am
Post subject:
Shouldn't any decent optimizer fix that, ekted?

Besides, readability is far more important that efficiency in ~90% of cases.
Mr Ekted - Sat Apr 02, 2005 12:42 am
Post subject:
I would agree in most cases, but if you are writing something that absolutely has to be killer fast--maybe you would even go so far as to profile it--then this is something you would seriously consider. A thousand loops, no big deal, A million, you are starting to get iffy.
Maverick - Sat Apr 02, 2005 8:47 am
Post subject:
Mr Ekted wrote:
Classic example (if you care about speed):

Code: Show/Hide
// bad code

for (i = 0; i < 1000000; i++)
   {
   if (something)
      Func1(i);
   else
      Func2(i);
   }

// good code

if (something)
   for (i = 0; i < 1000000; i++)
      Func1(i);
else
   for (i = 0; i < 1000000; i++)
      Func2(i);

Hm.. can you explain me that please? Why is running two identical loops better then only running one?
D1st0rt - Sat Apr 02, 2005 9:35 am
Post subject:
It only has to do the conditional branch once.
Maverick - Sat Apr 02, 2005 9:41 am
Post subject:
D1st0rt wrote:
It only has to do the conditional branch once.

The if() takes longer then the for loop? icon_confused.gif
D1st0rt - Sat Apr 02, 2005 9:42 am
Post subject:
In the first example, it has to do the if 1000000 times. In the second, it does it once.
Maverick - Sat Apr 02, 2005 9:43 am
Post subject:
aaaah... but two for loops of that size should still make up more cpu cycles then one (with the if() )? Or am I mistaken..
Cyan~Fire - Sat Apr 02, 2005 11:07 am
Post subject:
It'll be executing more than one command in ASM, if that's what you mean. But in both cases, the for loop commands stay the same, but the conditional code only has to be done once in the second, which obviously makes it faster no matter how long the for()s take.
D1st0rt - Sat Apr 02, 2005 12:57 pm
Post subject:
In the second example, only one of the loops gets run
SamHughes - Sat Apr 02, 2005 9:38 pm
Post subject:
What should I do if looping through branches a million times is my favorite form of gambling?
Bak - Sat Apr 02, 2005 10:43 pm
Post subject:
SamHughes wrote:
What should I do if looping through branches a million times is my favorite form of gambling?
end yourself
Maverick - Sun Apr 03, 2005 6:11 am
Post subject:
SamHughes wrote:
What should I do if looping through branches a million times is my favorite form of gambling?

Try russion roulette as your final gambling game.
All times are -5 GMT
View topic
Powered by phpBB 2.0 .0.11 © 2001 phpBB Group