Sign in to follow this  
Rasmadrak

How should loops be handled?

Recommended Posts

Just a quick question: Would this be slower/faster ...
 
for (int i=0; i < 5000; i++)
  {
    DoSomething();
    DoSomethingElse();
    DoSomethingReallyHeavy();
  }


than this...?

for (int i=0; i < 5000; i++)
    DoSomething();

for (int i=0; i < 5000; i++)
    DoSomethingElse();

for (int i=0; i < 5000; i++)
    DoSomethingReallyHeavy();


to be more precise; which would take most CPU time?

Share this post


Link to post
Share on other sites
It would be faster, because it has 3 times less jumps then the next example.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <windows.h>
#include <ctime>

void test(int ctr) {
for(int i(0); i<ctr; i++) {

}
}

int main(void)
{
DWORD start = GetTickCount();

for (int i = 0; i < 15000; i++) {
test(1000);
test(10000);
test(1000000);
}

std::cout << GetTickCount() - start << std::endl;
start = GetTickCount();

for(int i=0; i<15000; i++)
test(1000);
for(int i=0; i<15000; i++)
test(10000);
for(int i=0; i<15000; i++)
test(1000000);

std::cout << GetTickCount() - start << std::endl;

return 0;
}




Actually I just ran this test program a couple of times and the second version was faster (albeit barely: 24341 vs. 24152). Another advantage of the second version is it gives you the chance do perform anything you may need to do between functions.

Share this post


Link to post
Share on other sites
The 'overhead' of the loop is so small as to be inconsequential. The difference will be, at most, neglible.

Share this post


Link to post
Share on other sites
I think you'd also have to be careful that the two are semantically different, for anything other than a trivial example your functions would probably be modifying state so you wouldn't have much choice.

I guess what I'm saying is that in any realistic program the two are not a performance choice but constrained by the design.

Share this post


Link to post
Share on other sites
yeah, the two bits of code are two different things.

It's like doing this


for(int i = 0; i < 1000; i ++)
{
UpdateGame();
RenderGame();
WaitVsyncAndFlipBackBuffer();
}




for(int i = 0; i < 1000; i ++)
{
UpdateGame();
}
for(int i = 0; i < 1000; i ++)
{
RenderGame();
}

for(int i = 0; i < 1000; i ++)
{
WaitVsyncAndFlipBackBuffer();
}




The second bit of code doesn't make sense.

Share this post


Link to post
Share on other sites
I think it was just a general case for 3 functions that must be called 500 times each. You saw there was an obvious benefit to the first, but what you use will depend on what you want to do with them. If your functions is rendering frames of an animation, then you want the first. If your functions process 5000 inputs, then process physics on 5000 objects, then render 5000 objects then you want the second.

But you were just curious about the loop overhead, right?
Don't worry about the loop overhead! ... unless all those functions are inlined and have like 1 add operation inside them. Chances are they are much more complex than that. And the extra jumps won't be a big deal. Processor branch predictors are pretty good, so its not like the cpu is going to be flushing the pipeline every cycle.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this