efficency for not instanciating variables?

G

Gernot Frisch

Hi,

is this code:

class XY{};
void foo(bool b)
{
XY a;
if(b)
{
XY c;
c.Dosomething();
}
}

more efficient than:

void foo(bool b)
{
XY a, c;
if(b)
{
c.Dosomething();
}
}

if (!b), or will the compiler (gcc 3.3.3+, VC7.1) recognize it and
produce the same code?
Because I'm always writing such code and am not sure if the compiler
would have been smart enough to do that itself.
 
M

matthias_k

Gernot said:
Hi,

is this code:

class XY{};
void foo(bool b)
{
XY a;
if(b)
{
XY c;
c.Dosomething();
}
}

more efficient than:

void foo(bool b)
{
XY a, c;
if(b)
{
c.Dosomething();
}
}

if (!b), or will the compiler (gcc 3.3.3+, VC7.1) recognize it and
produce the same code?
Because I'm always writing such code and am not sure if the compiler
would have been smart enough to do that itself.

I think it's basically a good idea to consider lazy evaluation when
writing your code. That means, define your objects when you need them
(as opposed to ANSI-C for example, where you have to define all objects
you want to use in a function at the beginning of the function body).

If you only need to access c in the if-block, there is no reason to
define it outside. I seriously doubt that your compiler would optimize
it away, because the if-statement is evaluated at runtime, not at
compile time. How should your compiler know if it will be executed?
So unless you have a compiler with precognitive capabilities, it will
most probably not be optimized away (there may be exceptions though,
like redundant expressions like if(false) or such.. In those cases your
compiler will probably discard c).

Regards,
Matthias
 
P

Peter Koch Larsen

Gernot Frisch said:
Hi,

is this code:

class XY{};
void foo(bool b)
{
XY a;
if(b)
{
XY c;
c.Dosomething();
}
}

more efficient than:

void foo(bool b)
{
XY a, c;
if(b)
{
c.Dosomething();
}
}

if (!b), or will the compiler (gcc 3.3.3+, VC7.1) recognize it and produce
the same code?
Because I'm always writing such code and am not sure if the compiler would
have been smart enough to do that itself.
That question of course depends on the compiler, and some compilers with
global optimizations might produce the same code on both examples.
Still, for readability as well as speed, the first way is always preferable.
Do not in general declare variables at a scope where they are not needed.
If you replace the "if"-statement with a loop, it might in some cases be
faster to declare wour variable outside the loop, but notice that this
changes the semantics of the code.

/Peter
 
E

Efrat Regev

Gernot Frisch said:
Hi,

is this code:

class XY{};
void foo(bool b)
{
XY a;
if(b)
{
XY c;
c.Dosomething();
}
}

more efficient than:

void foo(bool b)
{
XY a, c;
if(b)
{
c.Dosomething();
}
}

if (!b), or will the compiler (gcc 3.3.3+, VC7.1) recognize it and
produce the same code?

1. XY's constructor might be in a different compilation unit, which, at the
very least, would make it difficult for the compiler to optimize
2. Personally, I think the two code snippets you wrote mean two different
things: the first says that an XY object c exists if b is true (and c should
do something). The second says that an XYobject c exists, and if b is true,
then c should do something. If you meant the former, then I think that you
should switch to your second snippet, for the same reason that global
variables should be avoided (i.e., I think you should always minimize the
scope of variables as muhc as possible).
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top