Jump to content
  • Advertisement

Archived

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

jfclavette

Loops

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

hypothetical: Would
  
long foo=0
long bar=0

long array[10000][5]= {3,6,2,6,8}

for (foo=0; foo<=9999; foo++)
{
  for (foo=0; foo<=5; foo++)
  {
    array[foo][bar]++;
  }
}
  
be faster than
  
long foo=0
long bar=0

long array[10000][5]= {3,6,2,6,8}

for (foo=0; foo<=9999; foo++)
{
  array[foo][0]++;
  array[foo][1]++;
  array[foo][2]++;
  array[foo][3]++;
  array[foo][4]++;
}
  
Assuming the situation is rigorously the same, but a little bit more complicated. Would the later be much faster?

Share this post


Link to post
Share on other sites
Advertisement
Theoretically the second should be faster. Also, I think this might be a little faster than the first example you gave.

  
long foo=0;
long bar=0;
long array[10000][5]= {3,6,2,6,8};
for (bar=0; bar<=5; bar++) {
for (foo=0; foo<=9999; foo++) {
array[foo][bar]++;
}
}

All I did was switch the inner and outer loops. Putting the loop with more iterations as the inner loop should be faster, I think. Also it might be possible to write it quite a bit faster using pointers rather than indexes into a 2D array. Maybe like this.

  
long array[10000][5]= {3,6,2,6,8};
long * p, * end = array + 49999;
for (p=array; p != end; p++)
(*p)++;
}


[edited by - Russell on April 2, 2003 6:00:16 PM]

Share this post


Link to post
Share on other sites
use < not <=, and yes the second case would be marginally faster, depending on what goes on inside the second loop.

It would also be faster to flip it around:

for(int j=0; j<5; ++j)
for(int i=0; i<10000; ++i)
stuff();

Share this post


Link to post
Share on other sites
Careful everyone,

read up on multidimensional arrays in your C++ help index!

array [ a ][ b ] implies a-rows by b-colums.

the b is direct indexing, so your loop should be (optimized):

for(a...) {
for(b...) {
array[ a ][ b ] = xxx;
};
};

and not the other way around like this:

for(b...) {
for(a...) {
array[ a ][ b ] = xxx;
};
};

This second example is an order of magnitude slower.

www.cppnow.com

[edited by - superdeveloper on April 2, 2003 11:58:44 PM]

Share this post


Link to post
Share on other sites
Don't forget about loop inversion*.. some machines are faster with decrementing and comparing with zero than incrementing and comparing with a certain maximum. Text both loops with your machine to see which runs faster.

    
foo = 10000
while (--foo)
{
array[foo][4]++;
array[foo][3]++;
array[foo][2]++;
array[foo][1]++;
array[foo][0]++;
}


* "C Unleashed" by SAMS is a great book.

(silencer)

[edited by - -silencer- on April 3, 2003 2:43:44 AM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!