Mombu the Programming Forum

Go Back   Mombu the Programming Forum > Programming > efficiency and uninitialized variables
User Name
Password
REGISTER NOW! Mark Forums Read




Reply Bookmark and Share
1 8th November 21:23
nick
External User
 
Posts: 1
Default 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! ]
  Reply With Quote


 


2 17th November 11:21
joe
External User
 
Posts: 1
Default 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:

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! ]
  Reply With Quote


 


3 17th November 11:22
karthik kumar
External User
 
Posts: 1
Default efficiency and uninitialized variables


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! ]
  Reply With Quote
4 17th November 11:22
patrick leslie polzer
External User
 
Posts: 1
Default efficiency and uninitialized variables


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! ]
  Reply With Quote
5 17th November 11:22
peter koch larsen
External User
 
Posts: 1
Default efficiency and uninitialized variables


"nick" <maskofzero@hotmail.com> skrev i en meddelelse
news:1106542546.765114.54470@f14g2000cwb.googlegro ups.com...

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! ]
  Reply With Quote
6 17th November 11:22
pete becker
External User
 
Posts: 1
Default efficiency and uninitialized variables


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! ]
  Reply With Quote
7 17th November 11:22
falk_tannhäuser
External User
 
Posts: 1
Default 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...


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

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! ]
  Reply With Quote
8 17th November 11:22
daniel_krügler_(ne_spangenberg)
External User
 
Posts: 1
Default efficiency and uninitialized variables


Hello Nick,

nick schrieb:

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! ]
  Reply With Quote
9 17th November 11:22
brangdon
External User
 
Posts: 1
Default efficiency and uninitialized variables


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! ]
  Reply With Quote
10 17th November 11:22
eda-qa mort-ora-y
External User
 
Posts: 1
Default efficiency and uninitialized variables


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! ]
  Reply With Quote
Reply


Thread Tools
Display Modes


Some other forums that might be of your interest : Development, Ada, Apple script, Assembler, Awk, Beos, Basic, C, C++, C#, C# .net, .net, .net frameworks, Asp .net, Clarion, Clipper, Clos, Clu, Cobol, Coldfusion, Delphi, Dylan, Eiffel, Forth, Fortran, Haskell, Hermes, Icon, Idl, Java, Java script, Jscript .net, Jcl, Linoleum, Lisp, Lotus, Limbo, Logo, Ml, Mumps, Oberon, Postscript, Pop, Pl1, Prolog, Python, Ruby, Pascal, Perl, Php, Rebol, Rexx, Sed, Sather, Scheme, Smalltalk, Tcl, Vhdl, Vrml, Visual basic, Visual basic .net, Yorick, Mysql, Omnis, Postgresql, Xbase, Access, Oracle, Adabas, Berkeley, Btrieve, Filemaker, Gupta, Db2, Informix, Ingres, Mssql server, Object, Olap, Paradox, Rdb, Revelation, Sybase, Theory, Dbase, Html, Java script, Css, Flash, Photoshop, Corel script, Xml, Tech, Beos, Gem, Hp48, Hpux, Linux, Mac, Ms-dos, Os2, Palm, Solaris, Ti99, Windows, Xenix, Aos, Chorus, Geos, Inferno, Lantastic, Lynx, Mach, Minix, Netware, Os9, Parix, Plan9, Psos, Qnx, Xinu, Sco, Unix, Aix, Aux, 386bsd, Bsdi, Freebsd, Netbsd, Openbsd, Ultrix, Amd, Intel, Aptiva, Buz, Deals, Homebuilt, Overclocking, Programming, Extra forums


Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666