I
Ivan Liu
Hi, I wonder if it's more efficient to put variable/object delaration
outside the for/do-while loop or it doesn't matter.
outside the for/do-while loop or it doesn't matter.
Ivan said:Hi, I wonder if it's more efficient to put variable/object delaration
outside the for/do-while loop or it doesn't matter.
Hi, I wonder if it's more efficient to put variable/object delaration
outside the for/do-while loop or it doesn't matter.
Frederick said:Ivan Liu posted:
If you have something like the following:
for(unsigned i = 0; i != 10; ++i)
{
MyClass obj;
}
, then the constructor is called 10 times, and the destructor is called 10
times.
If you have the following:
MyClass obj;
for(unsigned i = 0; i != 10; ++i)
{
}
, then the constructor is called once, and the destructor is called once.
I'd be quick to say that the latter would be more efficient (i.e. faster),
but then again it all depends on the classes your using, and what kind of
code the compiler makes.
Ivan said:So, which one is usually more costly? [..]
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.