In for loops, does the length of the array handle each iteration?

if I have a for loop, for example ...

for (int i = 0; i < myArray.length; i++) { ... } 

... does myArray.length evaluate each iteration? So something like ...

 int len = myArray.length; for (int i = 0; i < len; i++) { ... } 

... a slight increase in productivity?

+5
source share
4 answers

The first form is likely to entail some performance limitation, since its evaluation will require up to iflt , aload , arraylength and iload ; whereas the second is only two iload s.

@ajp rightly mentions that myArray may change; therefore, it is very unlikely that the compiler will optimize the first form in the second for you (if maybe myArray is final ).

However, the JIT, when it starts, is probably smart enough that if myArray does not change, it will turn the first form into the second.

Just in case, in any case, use the second form (this is what I always do, but it's just out of habit). Please note that you can always javap create the file of the generated file to see the generated bytecode and compare.


By the way, Wikipedia has a very convenient page that lists all the JVM bytecodes . As you can see, many of them are dedicated to arrays!

+3
source

whatever myArray.length is just a field, so evaluate

there is nothing.

The Java array has a length as public final int , so it is initialized once and when you reference it, there is no code execution, like a method call

The public final field length that contains the number of array components. length can be positive or zero.

+4
source

Yes, the termination expression gets evaluated every time. Thus, you are correct that preserving length once may be a small increase in performance. But more importantly, it changes the logic, which can make a difference if myArray gets reassigned.

 for (int i = 0; i < myArray.length; i++) { if (something-something-something) { myArray = appendToMyArray(myArray, value); // sets myArray to a new, larger array } } 

Now it matters a lot: first save the length of the array in a variable.

Usually you do not see such code with an array. But with an arrayList or other collection, the size of which can increase (or decrease) in the body of the loop, it makes the difference big if you calculate the size once or every time. This idiom appears in algorithms where you save a to-do list. For example, here is a partial search algorithm for everyone who is directly or indirectly associated with someone:

 ArrayList<Person> listToCheck = new ArrayList<>(KevinBacon); for (int i = 0; i < listToCheck.size(); i++) { List<Person> connections = allConnections(listToCheck.get(i)); for (Person p : connections) { if ([p has not already been checked]) { listToCheck.add(p); // increases listToCheck.size()!!! } } } 
+2
source

Not really. Both cases compare the value with two memory addresses with each iteration, except that you do unnecessary assignment when using the len variable. The performance difference is probably very small, and the first line is more readable, so I will use the first way, since it is more readable. If you want to be more readable and efficient, use the for-each loop if you are just going to perform a linear iteration through your array. For each cycle, they look like this:

 int [] myArray = {1,2,3}; for(int i:myArray){ System.out.print(i); } 

will print:

 1 2 3 

since I set for each element of the array. For each cycle it can be used for many objects, and this is a good function to study. Here is an explanation of this guide.

https://www.udemy.com/blog/for-each-loop-java/

0
source

Source: https://habr.com/ru/post/1211044/


All Articles