final sort tests
This commit is contained in:
parent
337a5dfbc8
commit
1f7bff62eb
|
@ -0,0 +1,39 @@
|
||||||
|
package com.baeldung.java.sort;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class CollectionsSortCompare {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
sortPrimitives();
|
||||||
|
sortReferenceType();
|
||||||
|
sortCollection();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sortReferenceType() {
|
||||||
|
Integer[] numbers = {5, 22, 10, 0};
|
||||||
|
Arrays.sort(numbers);
|
||||||
|
System.out.println(Arrays.toString(numbers));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sortCollection() {
|
||||||
|
List<Integer> numbersList = new ArrayList<>();
|
||||||
|
numbersList.add(5);
|
||||||
|
numbersList.add(22);
|
||||||
|
numbersList.add(10);
|
||||||
|
numbersList.add(0);
|
||||||
|
|
||||||
|
Collections.sort(numbersList);
|
||||||
|
|
||||||
|
numbersList.forEach(System.out::print);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sortPrimitives() {
|
||||||
|
int[] numbers = {5, 22, 10, 0};
|
||||||
|
Arrays.sort(numbers);
|
||||||
|
System.out.println(Arrays.toString(numbers));
|
||||||
|
}
|
||||||
|
}
|
|
@ -12,37 +12,30 @@ import java.util.List;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.SingleShotTime)
|
@BenchmarkMode(Mode.SingleShotTime)
|
||||||
@OutputTimeUnit(TimeUnit.MINUTES)
|
@OutputTimeUnit(TimeUnit.MILLISECONDS)
|
||||||
@Measurement(batchSize = 100000, iterations = 10)
|
@Measurement(batchSize = 100000, iterations = 10)
|
||||||
@Warmup(batchSize = 100000, iterations = 10)
|
@Warmup(batchSize = 100000, iterations = 10)
|
||||||
public class ArraySortBenchmark {
|
public class ArraySortBenchmark {
|
||||||
|
|
||||||
@State(Scope.Thread)
|
@State(Scope.Thread)
|
||||||
public static class Initialize {
|
public static class Initialize {
|
||||||
|
Integer[] numbers = {5, 22, 10, 0};
|
||||||
String[] words = {"Java", "Baeldung", "Tutorial"};
|
int[] primitives = {5, 22, 10, 0};
|
||||||
List<String> wordList = new ArrayList<>();
|
|
||||||
|
|
||||||
@Setup(Level.Trial)
|
|
||||||
public void setUp() {
|
|
||||||
wordList.add("Java");
|
|
||||||
wordList.add("Baeldung");
|
|
||||||
wordList.add("Tutorial");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public String[] benchmarkArraysSort(ArraySortBenchmark.Initialize state) {
|
public Integer[] benchmarkArraysIntegerSort(ArraySortBenchmark.Initialize state) {
|
||||||
Arrays.sort(state.words);
|
Arrays.sort(state.numbers);
|
||||||
return state.words;
|
return state.numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Benchmark
|
@Benchmark
|
||||||
public List<String> benchmarkCollectionsSort(ArraySortBenchmark.Initialize state) {
|
public int[] benchmarkArraysIntSort(ArraySortBenchmark.Initialize state) {
|
||||||
Collections.sort(state.wordList);
|
Arrays.sort(state.primitives);
|
||||||
return state.wordList;
|
return state.primitives;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
Options options = new OptionsBuilder()
|
Options options = new OptionsBuilder()
|
||||||
.include(ArraySortBenchmark.class.getSimpleName()).threads(1)
|
.include(ArraySortBenchmark.class.getSimpleName()).threads(1)
|
||||||
|
|
Loading…
Reference in New Issue