Java - Difference Between Loop Termination Expression

I'm just wondering: is there a difference in speed and performance between this two-loop implementation? Suppose that the size () method returns the length of an array, collection, or object that processes a group of elements (in fact, this is from the XOM api).

Implementation 1:

int size = someArray.size(); for (int i = 0; i < size; i++) { // do stuff here } 

Implementation 2:

 for (int i = 0; i < someArray.size(); i++) { // do stuff here } 
+4
source share
6 answers

In terms of performance, the difference is small. This is because the loop can be optimized so that the search by size () is embedded, resulting in a very small difference in performance.

The main difference is that the size changes during the cycle. The first case will try to iterate a fixed number of times. In the second case, the number of iterations will depend on the final size ().

+3
source

The first fragment will work faster since it only calls size() . The second fragment calls size() N times. Depending on the implant. this can create a significant penalty, especially. if the compiler complicates the built-in method and / or the size() method does not just return a non-volatile variable, etc.

I would rewrite it as for(int i=0, s=someCollection.size(); i<s; i++)

Note. There is no size() method in arrays.

+1
source

Yes, there is a difference. In the first loop, the size () method is called only once. In the second, at each iteration.

If the iteration resizes the collection (which is very unusual), a second one is required. In most cases, you should prefer the first, but limit the scope to a variable size:

 for (int i = 0, size = someArray.size(); i < size; i++) { // ... } 

But in most cases, you should prefer the foreach syntax:

 for (Foo foo : collection) { // ... } 

which will efficiently iterate over an array or collection, even for LinkedList, for example, if indexed access is not optimal.

+1
source

Don't worry about it, JVM optimization is very aggressive these days.

Use the 2nd form, for it is more readable and, most likely, just as fast. Premature optimization of yada yada.

And when you need to improve speed, always create a profile first, don't guess.

It is highly unlikely that caching size () in a local variable can benefit your application. If so, you should do simple operations on a huge dataset. In this case, you should not use an ArrayList.

0
source

It may be worth noting that this design:

 for (String s : getStringsList()) { //... } 

calls getStringsList() only once, and then runs on the iterator backstage. Therefore, it is safe to perform lengthy operations or change some state inside getStringsList() .

0
source

Always avoid anything that can be done outside the loop, like method calls, assigning values ​​to variables, or testing conditions.

A method call is more expensive than the equivalent code without a call, and repeating method calls over and over again, you simply add overhead to your application.

Move any method calls from the loop, even if you need to rewrite the code for this.

Benefits: -

If the compiler does not optimize it, the loop condition will be calculated for each iteration of the loop.

If the condition value does not change, the code will execute faster if the method call is removed from the loop.

Note: -

If the method returns a value that will not change during the cycle, store its value in a temporary variable until the cycle.

Therefore, its value is stored in the temporary size of the variable outside the loop, and then is used as a condition for ending the loop.

0
source

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


All Articles