does java compiler optimize this ...
does java compiler optimize that "something.length" in the below code during compile time or runtime(possible??) ::
Code:
for (int i=0;i<something.length;i++) { . |
Maybe it is better to do :
Code:
final int len = something.length; |
Compilers with optimisation would look to see if something.length get's modified in the block or any of the methods that are called within the block. If there is any chance that it would be modified then it would keep the code as it is, checking the value on each iteration, if on the other hand it knew that it was not going to be changed then it would try an optimise the code by keeping it in a register.
Although I don't know enough about the Java compiler to answer your question. I'm not convinced that mrcheeks solution would save any time, although if the condition was more complex then it might be a better approach. |
thanks for the reply ... i'm just being confused by some when they say its better to do what mrcheeks is doing while some other(although they never say it) simply just leave it and even for nested blocks with their own "someother.length" , but this "someother.length" of the nested blocks couldnt be understood yet during compile time ... i donno ...
i think its better for me to give a variable first before i understand how the for loop actually works ... . |
Quote:
|
It is just the fact that
Code:
something.length |
As I see it the potential danger of using a variable is; if within the loop, the loop counter may change its value. It probably doesn't in this case, but I suspect the example was given as that, an example.
Changing the example as follows: Code:
int listSize = ledger.length; Now consider this, you wrote the code before any banking wizard thought of direct debits as a means of reducing their administratve costs. They didn't exist. You substituted ledger.length with a variable for optimisation reasons. Now direct debits have been introduced and the code needs to be modified, it is given to newbie coder to implement,(whilst I agree there may be issues with how I've implemented it here, I am after all a newbie coder) I test my code, it works the items are added to the ledger, sign the code off and time to celebrate, job well done. Unfortunately because the code all runs and a total is calculated from the original loop it will probably be accepted for several months before someone realises that not all the ledger entries are being totaled up. Consider the second loop, it has the condition of Code:
ledger.myAccount().getEOMDDList().length() Code:
EOMDDList eomDDList = ledger.myAccount().getEOMDDList() Anyway I hope that helps, and for what it is worth; when you can leave the optimisation to the compiler cheers, graeme. |
>> "is computed all the time in the loop"
kind of like what i need to know(or confirm ??) initially ... and this brings me to graemef's ledger.myAccount().getEOMDDList().length() does the conditioning really computes them everytime in a loop even if that something.length is a method call similar to graemef's assuming that it is also possible to do that in a loop ?? //thanks to all for being patient ... . |
The original post was talking about an array. Arrays have a fixed length, so you don't have to worry about it changing. But I doubt there is a performance difference between accessing "something.length", which is a member of the array object, and a variable.
For elegance, in Java 1.5, if "something" was an Foo[], you can do this: Code:
for (Foo x : something) { |
Quote:
graeme. |
>> for (Foo x : something) {
its good for me to know theres a new "style"(lacking of word) in doing things but i prefer to remain as "procedural" as possible at this initial stage ... so i guess that the compiler will just leave it as it is(without optimization) if it couldnt understand the condition during compile time ... and ... a for loop always computes the condition throughout it iteration if its a method call to get an integer value(or something) during runtime ... i think by giving a variable value is good for me sometime ... . |
graemef & spooon hit what i was talking about. the length member is not calculated every time it is accessed, but rather is changed when the array size is set/changed, so there should be no difference btw using a local variable and the array member length, except for the side effects of a change in length, in which case the length is the safe way to go..
hmmm.. sounds like i just repeated alot of stuff that was already said... but im leaving it anyhow. :) |
All times are GMT -5. The time now is 06:05 AM. |