1
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class CompareList {
public static void main(String[] args) {
 List<Integer> ArrayList = new ArrayList<Integer>();
 List<Integer> LinkedList = new LinkedList<Integer>();
 doCalculations("ArrayList",ArrayList);
 doCalculations("LinkedList",LinkedList); 
}
private static void doCalculations(String type,List<Integer> List){
 for(int i=0;i<1E5;i++){
 List.add(i); 
 }
 Long start = System.currentTimeMillis();
 /*
 To add elements in the end 
 for(int i=0;i<1E5;i++){
 List.add(i); 
 }
 */
 for(int i=0;i<1E5;i++){
 List.add(0,i);
 }
 Long end = System.currentTimeMillis();
 System.out.println("time taken" +" "+ (end-start) + "ms for" +" "+ type);
}
}

time taken 13ms for ArrayList time taken 64ms for LinkedList

i know this is duplicate question ,but please don't remove this ,whatever answers they gave to this question i was unable understand ,Can anyone explain this in simple words why this linked list is slower when we add element in the end?

Luiggi Mendoza
85.9k16 gold badges160 silver badges355 bronze badges
asked May 15, 2014 at 17:33
9
  • 5
    Because benchmarking is hard. stackoverflow.com/questions/504103/… Commented May 15, 2014 at 17:34
  • Sorry but your test is completely broken. See stackoverflow.com/questions/504103/… Commented May 15, 2014 at 17:34
  • Try reversing the call of doCalculations in your main method and you will get completely different results ;) Commented May 15, 2014 at 17:34
  • @LuiggiMendoza what do you mean by reversing? Commented May 15, 2014 at 17:35
  • Call doCalculations("LinkedList",LinkedList) first, then doCalculations("ArrayList",ArrayList); and you'll see. Commented May 15, 2014 at 17:36

2 Answers 2

4

What you're currently doing here is a micro benchmark. This is not an easy task, because you have to take into account all these tips: How do I write a correct micro-benchmark in Java?.

Still, there's no need to reinvent the wheel. There are frameworks that ease this work like JUnitBenchmarks and Caliper that help you to perform real benchmarks for your pieces of code/algorithms by using JUnit tests.

answered May 15, 2014 at 17:40
Sign up to request clarification or add additional context in comments.

Comments

0

linked list is slower cuz it is like a train.......it have to check each element and check it is value ..until it find the matched element..so think of it like chained train and you want to go from the first of the train to the end

answered May 15, 2014 at 23:02

1 Comment

but then why its faster ?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.