I'm primarily concerned with C compilers and sun's JDK, but general info is more than welcome (Documentation for self-education as well).
Well, if i wrote a program like
int i = 5
printf("i is $d", i);
Would i=5 be stored separately in memory, or would it exactly the same the following?
printf("i is 5");
(perhaps ("i is $d", 5) is more accurate)
If not, one might balance readablity/maintainability against performance (small improvement perhaps, but I'm a sucker for learning these things, if not using them :) )
An optimizing compiler would likely choose your final option, assuming there's nothing in the code that would require i to exist as an actual entity. There are a huge number of factors involved when it comes to optimization though, so without a full example it's hard to tell you what the possible outcomes are.
There are a huge number of factors involved when it comes to optimization though, so without a full example it's hard to tell you what the possible outcomes are.
Okay, I'm quite happy just having learned that "optimizing compilers" probably will optimize this.
So, if I may lean on the boundaries of between the computer science and C forum, I'm guessing with gcc there will be no optimization, unless called with -O or similar parameters? :)
EDIT: Karlwakim: perhaps I should have made it more pseudo code-ish the code is just this
Variable = 5
print-to-std-output(Variable is *VALUE_OF_Variable*);
which the compiler might see is the same as print-to-std-output(Variable is 5); As no operations are done to the variable. It is only kept there for readability/maintainability :)