Some interesting performance gain which I encountered while writing some test code and thought of sharing with you people.
Let us look at result of this example:
As you can see Arrays.asList took 1/3rd time when compared against list.add method.
(Note: processing time will vary, but asList method will always take less time when compared to add method.)
It is time to know the reason why add method takes more time if compared against Arrays.asList method.
If you check implementation of ArrayList add method, for each add method call array list checks for capacity using ensureCapacity method. So this is an overhead if you want fixed size list for some specific purpose.
That's all from my end on this topic. Looking forward for your suggestions and comments.
May be you encountered situation like you have an array in your hand and some method need to be called which takes java.util.List as input parameter. In that case you need to convert that array into list and then call the method.
Here I am assuming that target method doesn't want to change anything in input list. List content will be used only for method processing.
Now this can be achieved in 2 ways:
1. Create one arrayList, iterate through array and populate arrayList using add method,
2. Using Arrays.asList method to create a fixed size arrayList.
Here second way is far better than first way in terms of performance.
We will take one example to know performance gain achieved using second way.
package com.example.performance;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
int maxNumber = 10;
long startTime = System.nanoTime();
List<Integer> numbers = new ArrayList<Integer>();
for(int i=1; i<=maxNumber; i++){
numbers.add(i);
}
long endTime = System.nanoTime();
long timeWithArrayList = endTime - startTime;
System.out.println("Time taken to populate array list using add method: "+timeWithArrayList);
startTime = System.nanoTime();
Integer[] backingArray = new Integer[maxNumber];
for(int i=1; i<=maxNumber; i++){
backingArray[i-1]=i;
}
numbers = Arrays.asList(backingArray);
endTime = System.nanoTime();
long timeWithArraysAsList = endTime - startTime;
System.out.println("Time taken to populate array list using Arrays asList method : "+timeWithArraysAsList);
}
}
Let us look at result of this example:
Time taken to populate array list using add method: 786449
Time taken to populate array list using Arrays asList method : 199285
(Note: processing time will vary, but asList method will always take less time when compared to add method.)
It is time to know the reason why add method takes more time if compared against Arrays.asList method.
If you check implementation of ArrayList add method, for each add method call array list checks for capacity using ensureCapacity method. So this is an overhead if you want fixed size list for some specific purpose.
That's all from my end on this topic. Looking forward for your suggestions and comments.
No comments:
Post a Comment