Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


When to Declare Variables in Tight C++ Loops


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 James Leighe   Members   -  Reputation: 222

Like
0Likes
Like

Posted 09 June 2011 - 06:08 AM

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!

Sponsor:

#2 Hodgman   Moderators   -  Reputation: 31121

Like
0Likes
Like

Posted 09 June 2011 - 06:13 AM

They're both the same.

"Object*" is a primitive type, so there's no cost for "creating" one every iteration.

If instead you had an "Object" 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


#3 Ezbez   Crossbones+   -  Reputation: 1164

Like
6Likes
Like

Posted 09 June 2011 - 06:16 AM

I prefer the second method because it makes it obvious that each iteration has a new instance and that the instance is not supposed to be an output of the loop to be used afterwards.


If you're concerned about performance, try both and measure which is faster. If you can't measure the difference, then it doesn't which is 'faster.' My guess is that a good compiler optimizes both to be the same.

#4 siavash   Members   -  Reputation: 139

Like
0Likes
Like

Posted 09 June 2011 - 06:34 AM

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.)

#5 Martins Mozeiko   Crossbones+   -  Reputation: 1422

Like
3Likes
Like

Posted 09 June 2011 - 06:38 AM

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


#6 Burnt_Fyr   Members   -  Reputation: 1245

Like
0Likes
Like

Posted 10 June 2011 - 05:18 AM

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?

#7 wqking   Members   -  Reputation: 756

Like
0Likes
Like

Posted 10 June 2011 - 06:28 AM

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.

http://www.cpgf.org/
cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.
v1.5.5 was released. Now supports tween and timeline for ease animation.


#8 procgen   Members   -  Reputation: 104

Like
0Likes
Like

Posted 10 June 2011 - 08:18 AM

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.


void Function (void)

{

	int a;



	for (int i = 0; i < 1234; i++) 

	{

 		float b;

 		

 		b = SomeOtherFunction();

	}

}


Is going to turn into:

void Function (void)

{

	int a;

	float b;



	for (int i = 0; i < 1234; i++) 

	{

 		b = SomeOtherFunction();

	}

}



#9 rip-off   Moderators   -  Reputation: 8526

Like
2Likes
Like

Posted 10 June 2011 - 09:05 AM

@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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS