![]() |
|
|
|
|
1
8th November 21:23
External User
Posts: 1
|
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! ] |
|
|
|
|
2
17th November 11:21
External User
Posts: 1
|
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! ] |
|
|
|
|
3
17th November 11:22
External User
Posts: 1
|
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! ] |
|
|
4
17th November 11:22
External User
Posts: 1
|
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! ] |
|
|
5
17th November 11:22
External User
Posts: 1
|
"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! ] |
|
|
6
17th November 11:22
External User
Posts: 1
|
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! ] |
|
|
7
17th November 11:22
External User
Posts: 1
|
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! ] |
|
|
8
17th November 11:22
External User
Posts: 1
|
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! ] |
|
|
9
17th November 11:22
External User
Posts: 1
|
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! ] |
|
|
10
17th November 11:22
External User
Posts: 1
|
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! ] |
|