jugLviv

Meta


Share on:


Performance comparison of different for loops in java

juglvivjuglviv
In this post, we will compare the performance of different for loops in java

Different ways to use for loop

1) For each statement

In this technique, advanced for each statement introduced in java 5 is used.

 
private static List<Integer> list = new ArrayList<>();
for(Integer i : list)
{
   
// code stuff
}

2) Using list.size() in condition
&nbsp;
private static List<Integer> list = new ArrayList<>();
for(int j = 0; j < list.size() ; j++)
{
   
// code stuff
}
3) Initialize another local variable with size 
private static List<Integer> list = new ArrayList<>();
int size = list.size();
for(int j = 0; j < size ; j++)
{
   
// code stuff
}
4) Initialize the initial value of counter to size of list
private static List<Integer> list = new ArrayList<>();
for(int j = list.size(); j > size ; j--)
{
   
// code stuff
}

Comparing the performance of all types

import java.util.List;
import java.util.Calendar;
import java.util.ArrayList;
 
public class ForLoopPerformance
{

   
private static long startTime;
   
private static long endTime;

   
private static List<Integer> list = new ArrayList<>();

   
static
   
{
       
for(int i=0; i < 10000000; i++)
       
{
            list
.add(i);
       
}
   
}

   
public static void main(String[] args)
   
{
       
//Type 1
        startTime
= Calendar.getInstance().getTimeInMillis();
       
for(Integer i : list)
       
{
           
//
       
}
        endTime
= Calendar.getInstance().getTimeInMillis();
       
System.out.println("For each loop :: " + (endTime - startTime) + " ms");
 
       
//Type 2
        startTime
= Calendar.getInstance().getTimeInMillis();
       
for(int j = 0; j < list.size() ; j++)
       
{
           
//
       
}
        endTime
= Calendar.getInstance().getTimeInMillis();
       
System.out.println("Using collection.size() :: " + (endTime - startTime) + " ms");
 
       
//Type 3
        startTime
= Calendar.getInstance().getTimeInMillis();
       
int size = list.size();
       
for(int j = 0; j < size ; j++)
       
{
           
//
       
}
        endTime
= Calendar.getInstance().getTimeInMillis();
       
System.out.println("Using [int size = list.size(); int j = 0; j < size ; j++] :: "
       
+ (endTime - startTime) + " ms");
 
       
//Type 4
        startTime
= Calendar.getInstance().getTimeInMillis();
       
for(int j = list.size(); j > size ; j--)
       
{
           
//
       
}
        endTime
= Calendar.getInstance().getTimeInMillis();
       
System.out.println("Using [int j = list.size(); j > size ; j--] :: "
       
+ (endTime - startTime) + " ms");
   
}
}

Run it :

For each loop :: 116 ms 
Using collection.size() :: 36 ms 
Using [int size = list.size(); int j = 0; j < size ; j++] :: 4 ms 
Using [int j = list.size(); j > size ; j–] :: 1 ms


Reason for difference in performance

Type 1 is costliest one and simple reasoning is the use of iterator internally created in for each loop. Creating an iterator and calling iterator.get() adds up to most of cost which is not involved in direct access in other three types.
Type 2 uses size() method call every time and thus on runtime it brings a little overhead. Though JVM optimizes this code as inline call and other optimizations also and size method is simply a getter for size attribute of instance of list. Even though it brings a few more statements to execute at machine level code and which makes the difference.
Type 3 and 4 have a very little difference and should be considered as same, they both fetch the size of collection initially. And then uses this size value in loop for checking the condition.


Read more at http://www.simplecodestuffs.com/performance-comparison-of-different-for-loops-in-java/#wLxjCwsy23itWBoC.99

juglviv
Author