Jump to content
  • Advertisement
Sign in to follow this  
jamesleighe

When to Declare Variables in Tight C++ Loops

This topic is 2572 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

Is is better to do say:


Object* object;

for (int i = 0; i < 1000000; ++i)
{
object = GetSomeObject (i);
*do stuff with object*
}


or


for (int i = 0; i < 1000000; ++i)
{
Object* object;

object = GetSomeObject (i);
*do stuff with object*
}


The second way it seems that I have to recreate the variable for every loop, but is this really true? Also, remember that a practical use would involve many variables being 'recreated'.

Thanks!

Share this post


Link to post
Share on other sites
Advertisement
They're both the same.

"[font="Courier New"]Object*[/font]" is a primitive type, so there's no cost for "creating" one every iteration.

If instead you had an "[font="Courier New"]Object[/font]" it would be different -- e.g.:class Object
{
public:
Object() {
//do something expensive here
}
~Object() {
//do something expensive here
}
};
...
for (int i = 0; i < 1000000; ++i)
{
Object object;//calls Object::Object() every iteration

object = GetSomeObject (i); //calls Object::operator=(const Object&)

}//calls Object::~Object() every iteration

Share this post


Link to post
Share on other sites
I think second one should perform better in a multithreaded code, because threads aren't too much happy with global variables. (Please correct me if I'm wrong.)

Share this post


Link to post
Share on other sites
In C++ you should prefer initialization over assignment.
for (int i = 0; i < 1000000; ++i)
{
Object* object = GetSomeObject(i);
*do stuff with object*
}

Share this post


Link to post
Share on other sites

In C++ you should prefer initialization over assignment.
for (int i = 0; i < 1000000; ++i)
{
Object* object = GetSomeObject(i);
*do stuff with object*
}



sorry to hijack, but why and where can I find more about initialization vs assignment?

Share this post


Link to post
Share on other sites
Declare local variable as close to its using code as possible.
Put local variable only to the scope to the one real use it.
So I prefer to second one.

But as some other mentioned, object is different, but object pointer is not.

Share this post


Link to post
Share on other sites
Since a loop has no stack of its own I'm pretty sure both of the examples you gave turn into the same code after the compiler is done processing it.

[source]
void Function (void)
{
int a;

for (int i = 0; i < 1234; i++)
{
float b;

b = SomeOtherFunction();
}
}
[/source]
Is going to turn into:
[source]
void Function (void)
{
int a;
float b;

for (int i = 0; i < 1234; i++)
{
b = SomeOtherFunction();
}
}
[/source]

Share this post


Link to post
Share on other sites
@procgen
Stack? It might end up in registers, as constants or eliminated entirely depending on the nature of SomeOtherFunction().

@OP
Modern compilers are extremely good. If you're worried about speed you'll want to be looking at the high level algorithms and data structures you are using. In your example, you might be able to avoid processing all 1,000,000 objects with a clever data structure that groups objects spatially, for example. This would blow any low level optimisations out of the water.

When you are doing such low level optimisations, you'll need to be profiling and looking at the assembly. Without doing so, you're effectively stumbling in the dark and hoping your program will be faster when you're done.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!