| View previous topic :: View next topic |
| Author |
Message |
nick Guest
|
Posted: Mon Jan 24, 2005 11:23 am Post subject: efficiency and uninitialized variables |
|
|
I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Karthik Kumar Guest
|
Posted: Mon Jan 24, 2005 8:27 pm Post subject: Re: efficiency and uninitialized variables |
|
|
nick wrote:
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
This essentially depends on if you want to use the value
of 'a' between
int a = 0;
// do you want to use 'a' somewhere here.
// then probably a better idea to assign
// it a value in the beginning.
// Else a better option would be
// to declare 'a' at the point of invocation.
// something like,
// int a = dosomething();
//
//
a = dosomething();
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Joe Guest
|
Posted: Mon Jan 24, 2005 8:28 pm Post subject: Re: efficiency and uninitialized variables |
|
|
Actually, it's one of the reasons that C++ allows you to put variable
declarations anywhere you wish. What I would do is instead of:
nick wrote:
| Quote: | int a=0;
a = dosomething();
|
or
| Quote: | int a;
a = dosomething(); // somewhere further down
|
I would just use
int a = dosomething(); // somewhere right before I was going to use a
That way, you don't have to pay for an initialization that will never
be used and the compiler will warn you about uses of 'a' before you
intended to use it. Let the compiler help you as much as possible, and
don't do more work than you need to.
joe
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter Koch Larsen Guest
|
Posted: Mon Jan 24, 2005 8:28 pm Post subject: Re: efficiency and uninitialized variables |
|
|
"nick" <maskofzero (AT) hotmail (DOT) com> skrev i en meddelelse
news:1106542546.765114.54470 (AT) f14g2000cwb (DOT) googlegroups.com...
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
Your second version will likely be faster. I doubt this is something that
you could measure, though. But why not the obvious
int a = dosomething()?
If it is possible, this is the solution to go for.
/Peter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Patrick Leslie Polzer Guest
|
Posted: Mon Jan 24, 2005 8:32 pm Post subject: Re: efficiency and uninitialized variables |
|
|
nick wrote:
| Quote: | I usually init my variables
[...]
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
Assuming that the O/S does not blank the .data area automatically |
(and I don't think the major operating systems do that) it is
of course more efficient to leave superfluous assignments out
(thus saving the operations "Load into register" ->
"Modify value" -> "Save to memory").
However, compiler should not have any problems finding out that
this is superfluous (as in the example above the variable is
not volatile).
Leslie
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Mon Jan 24, 2005 8:35 pm Post subject: Re: efficiency and uninitialized variables |
|
|
nick wrote:
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
It's more efficient to initialize a variable once instead of
initializing it once and assigning to it once. The way to do that is:
int a = dosomething(); // somewhere further down
It's also clearer, because it doesn't initialize the variable with a
value that won't be used.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Falk Tannhäuser Guest
|
Posted: Mon Jan 24, 2005 8:37 pm Post subject: Re: efficiency and uninitialized variables |
|
|
The classical way to define and initialise a variable is
int a = dosomething();
Any C++ tutorial worth its name should explain this at the
very beginning...
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
|
This is only useful if the assignment is done conditionally,
as for example in
int a = 0;
if(some_condition)
a = dosomething();
use(a);
or
int a = 0;
try
{
a = dosomething();
}
catch(...)
{
... // Error handling
}
use(a);
| Quote: |
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
Defining uninitialised variables that are assigned somewhere
later should be avoided whenever possible, except in particular
cases as in
std::cout << "Enter a number: ";
int a;
std::cin >> a;
if(!std::cin)
... // Error handling
else
use(a);
Falk
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (ne Spange Guest
|
Posted: Mon Jan 24, 2005 8:42 pm Post subject: Re: efficiency and uninitialized variables |
|
|
Hello Nick,
nick schrieb:
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
From the point of micro-optimization this situation should not be worth |
worring about
and usually the compiler will produce the same code.
The **real** question arises, why you don't use the C++ way and simple write
int a = dosomething();
or, if you don't modify a in the following code:
const int a = dosomething();
which reduces the risk of variable abuses and is clean, tight, and
self-documenting code
;-)
Greetings from Bremen,
Daniel
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
edA-qa mort-ora-y Guest
|
Posted: Mon Jan 24, 2005 8:44 pm Post subject: Re: efficiency and uninitialized variables |
|
|
nick wrote:
| Quote: | int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
With a non-optimizing compiler it may be marginally more efficient
(saves 1 short CPU instruciton on most architectures). With an
optimizing compiler this is most likely done automatically (it won't
bother assinging unused values, and indeed will likely give you a
warning about it being unused).
--
edA-qa mort-ora-y
Idea Architect
http://disemia.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Mon Jan 24, 2005 8:45 pm Post subject: Re: efficiency and uninitialized variables |
|
|
[email]maskofzero (AT) hotmail (DOT) com[/email] (nick) wrote (abridged):
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
|
Conceptually, yes. In practice, usually no. It depends on the compiler,
but most compilers should optimise away the inefficiency, at least for
ints in simple situations. If the situation isn't simple, eg "if"
statements are involved, then the extra safety of the zero initialisation
might be worth while anyway. For more complex types, eg a smart pointer or
std::string, it's harder to be sure about what the compiler will be able
to optimise.
Generally it is better to defer declaring the variable until its initial
value is known:
int a = dosomething();
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Jan 24, 2005 10:37 pm Post subject: Re: efficiency and uninitialized variables |
|
|
nick wrote:
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
Actually
int a = dosomemething();
is the most efficient of all.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Nicola.Musatti@ObjectWay. Guest
|
Posted: Mon Jan 24, 2005 10:41 pm Post subject: Re: efficiency and uninitialized variables |
|
|
What makes 0 better than any random value? In general I don't like my
code to contain statements unrelated to its purpose; in the case of
native types it is not much a question of performance as those are
likely to get initialized at program startup in a very efficient way;
rather, it is a matter of making the meaning of my code as easy to
understand as possible.
Note also that in C++ variables do not have to be declared at the
beginning of a block, so it is usually possible to initialize them in a
useful way, e.g. in your example one may often move the declaration to
the point of first use.
By the way, if you really prefer to initialize your variables, why not
use a more recognisable pattern? Consider also that 0 is a rather
common value to find in unitialized memory.
Cheers,
Nicola Musatti
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jcoffin@taeus.com Guest
|
Posted: Mon Jan 24, 2005 10:41 pm Post subject: Re: efficiency and uninitialized variables |
|
|
nick wrote:
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
It may be more efficient (but then again it may not -- the compiler may
skip assigning the initial value if it notices that it's never used).
IMO, an improvement over either one is (if possible) to delay defining
the variable until the point at which it can be initialized:
int a(dosomething());
The obvious time this can't be done is if you have to initialize it
inside of some inner block:
int a;
if (x)
a = dosomething();
else
a = dosomething_else();
This can sometimes be worked around with something like:
int a = x ? dosomething() : dosomething_else();
--
Later,
Jerry.
The universe is a figment of its own imagination.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Skandinavisches Seminar Guest
|
Posted: Mon Jan 24, 2005 11:53 pm Post subject: Re: efficiency and uninitialized variables |
|
|
"nick" <maskofzero (AT) hotmail (DOT) com> schrieb im Newsbeitrag
news:1106542546.765114.54470 (AT) f14g2000cwb (DOT) googlegroups.com...
| Quote: | I usually init my variables
eg.
int a=0;
|
I apologize if the following is obvious to you.
I usually don't bother to initialize variables of plain data types, if I'm
declaring them before I use them for some reason. However, I always do so
for pointers, e.g.,
int a; // I don't care, not initialized.
int* p = 0;
If you forget yourself and dereference an unitialized pointer, you are
likely to get a segmentation fault. According to my understanding of the
matter, if the contents of `p' are by chance a valid address, then you
won't get one, but this would be even worse, especially if you're assigning
to `*p', whereas the compiler will catch it if you try to dereference a null
pointer. (I can't test this on this machine---no compiler.)
Laurence Finston
http://www.gnu.org/software/3dldf/LDF.html
Reply To: [email]lfinsto1 (AT) gwdg (DOT) de[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Mon Jan 24, 2005 11:54 pm Post subject: Re: efficiency and uninitialized variables |
|
|
nick wrote:
| Quote: | I usually init my variables
eg.
int a=0;
a = dosomething();
but i am wondering if it may be more efficient to just
int a;
a = dosomething(); // somewhere further down
even if it is only marginally more efficient it would be good
to know.
|
On Intel platform, using both MSVC 6.0 and 7.1, here's what I got:
// In debug mode
int a=0; // 7 bytes to initialize
a = dosomething(); // 8 bytes to call and store result
int b; // NO code generated
b = dosomething(); // 8 bytes to call and store result
int c = dosomething(); // 8 bytes
int d(dosomething()); // 8 bytes
// In release mode
int a=0; // NO bytes
a = dosomething(); // 7 bytes to call and store result
int b; // NO bytes
b = dosomething(); // 7 bytes to call and store result
int c = dosomething(); // 7 bytes
int d(dosomething()); // 7 bytes
No difference at all, except in debug mode. (MSVC 7.1 wanted to
inline dosomething()... I moved it into a different compilation
unit to defeat this.)
Other compilers might be slightly different... I wouldn't
worry about it.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|