Sign in to follow this  

question about constants

This topic is 4696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

which is faster this int doSomething(int x) { return x; } or this int doSomething(final/const int x) { return x; } or this int doSomething(int x) { return 5; } my question is really, does the compiler optimize constants so that it the program doesnt have to acces memory when it uses the constant.

Share this post


Link to post
Share on other sites
Perhaps when they are passed by address. Depends on the compiler. But, if your passing by reference then I don't think it makes one bit of difference.

[edit] Arrrrrgh! Dsylexia strikes again! I'll bet what originally posted didn't make one damn bit on sense =) [/edit]

[Edited by - lack o comments on February 7, 2005 11:42:40 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Riviera Kid
my question is really, does the compiler optimize constants so that it the program doesnt have to acces memory when it uses the constant.
No. You can still pass a const variable to that function (as opposed to a literal), which requires memory access internally.

More importantly, get out of the habit of needless microoptimization and voodoo programming. Write your program, make sure it is correct (fulfills all the design constraints and requirements), profile it, then optimize the portions where the profiler indicates it is spending the most runtime. Once it is sufficiently performant, stop. Optimization tends to make code ugly, unintuitive and harder to manage, so never do more than is necessary.

Share this post


Link to post
Share on other sites
Compiler? What compiler? What language are you talking about?
I assume 'final' must be a java keyword or something?

If you ever want to if something is faster then your best option is always to simply profile it.
None of these functions really do anything, so with such limited examples there is not even any point in speculating on the various relative speeds.

Basically if you are ever dealing with something that shouldn't be modified, make it const (as it should be), and the compiler may optimise it more in some way for you. If you can't make it const then you obviously dont. Either way, what optimisation the compiler will or will not do is irrelevant, as your program must function correctly, above all else.
What the compiler actually does is not of your concern. Your job is simply to adhere to the rules of the language itself.

Share this post


Link to post
Share on other sites

This topic is 4696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this