0

I hope I'm posting in the right forum :)

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

Edited by Bladtman242: n/a

3
Contributors
5
Replies
7
Views
5 Years
Discussion Span
Last Post by Bladtman242
-1

Sincerely i didn't understand your code,im new to programming.
Usually, even if not used, the variables are stored in memory.

0

I hope I'm posting in the right forum :)

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.

0

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

Edited by Bladtman242: n/a

2

I'm guessing with gcc there will be no optimization, unless called with -O or similar parameters?

It's best not to guess. GCC is very aggressive when it comes to optimization, so a test using assembly output would be the better approach.

Votes + Comments
This is great :)
0

I had no idea gcc could do that, that is awesome! :)
Not that assembly is my strongest, but I guess it's an opportunity to learn eh?
Thanks a bunch, I'll be looking into this :)

Edited by Bladtman242: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.