question about constants
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.
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]
[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]
Quote:Original post by Riviera KidNo. You can still pass a const variable to that function (as opposed to a literal), which requires memory access internally.
my question is really, does the compiler optimize constants so that it the program doesnt have to acces memory when it uses the constant.
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.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement