ArrayList vs LinkedList sorting speed?

package org.example;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

public class SortExample {
public static void main(String[] args) {
List < Integer > numbers = fillWithLotsOfRandomValues(10_000_000);
for (int i = 0; i < 10; i++) {
testSortingSpeed(numbers);
}
}

private static void testSortingSpeed(List < Integer > numbers) {
List < Integer > arrayList = new ArrayList < > (numbers);
List < Integer > linkedList = new LinkedList < > (numbers);
sortList(arrayList);
sortList(linkedList);
}

private static void sortList(List < Integer > list) {
long time = System.currentTimeMillis();
list.sort(null);
time = System.currentTimeMillis() - time;
System.out.println(list.getClass().getSimpleName() + " time = " + time);
}

private static List < Integer > fillWithLotsOfRandomValues(int n) {
return ThreadLocalRandom.current().ints(n).parallel().boxed()
.collect(Collectors.toList());
}
}
package org.example;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;

public class SortExample {
public static void main(String[] args) {
List < Integer > numbers = fillWithLotsOfRandomValues(10_000_000);
for (int i = 0; i < 10; i++) {
testSortingSpeed(numbers);
}
}

private static void testSortingSpeed(List < Integer > numbers) {
List < Integer > arrayList = new ArrayList < > (numbers);
List < Integer > linkedList = new LinkedList < > (numbers);
sortList(arrayList);
sortList(linkedList);
}

private static void sortList(List < Integer > list) {
long time = System.currentTimeMillis();
list.sort(null);
time = System.currentTimeMillis() - time;
System.out.println(list.getClass().getSimpleName() + " time = " + time);
}

private static List < Integer > fillWithLotsOfRandomValues(int n) {
return ThreadLocalRandom.current().ints(n).parallel().boxed()
.collect(Collectors.toList());
}
}
According to: https://stackoverflow.com/questions/8069370/is-an-arraylist-or-a-linkedlist-better-for-sorting:
"With Java 8, using an ArrayList should be slightly faster because Collections.sort will call List.sort and ArrayList has a specialised version that sorts the backing array directly, saving a copy.

So bottom line is ArrayList is better as it gives a similar or better performance depending on the version of Java."
"With Java 8, using an ArrayList should be slightly faster because Collections.sort will call List.sort and ArrayList has a specialised version that sorts the backing array directly, saving a copy.

So bottom line is ArrayList is better as it gives a similar or better performance depending on the version of Java."
Doesn't seem to be that impressive of a difference when measuring. Why?
No description
9 Replies
JavaBot
JavaBot9mo ago
This post has been reserved for your question.
Hey @Steadhaven! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
0x150
0x1509mo ago
what? yes it is java itself is just not accurate for benchmarking because of gc and jit
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
Steadhaven
SteadhavenOP9mo ago
>well written code btw! I can't take the credit :) I just found it and thought it was a nice code, but wondered why both ArrayList and LinkedList were so close :)
Unknown User
Unknown User9mo ago
Message Not Public
Sign In & Join Server To View
0x150
0x1509mo ago
this depends on the os, and it's not a java problem. you also dont need to have perfect real world accuracy for benchmarking, just a constant interval between two points in time
tjoener
tjoener9mo ago
If you want to benchmark, use JMH. Second, yes like bogrel said, arraylist is preffered in 99% of cases. Benchmarks in Java are hard to do. So don't just slap some nanotimes here and there and expect it to be correct. It takes a while for the JVM to optimize code etc
JavaBot
JavaBot9mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?