final sort tests

This commit is contained in:
mherbaghinyan 2019-02-19 13:28:30 +04:00
parent 337a5dfbc8
commit 1f7bff62eb
2 changed files with 49 additions and 17 deletions

View File

@ -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));
}
}

View File

@ -12,37 +12,30 @@ import java.util.List;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MINUTES)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Measurement(batchSize = 100000, iterations = 10)
@Warmup(batchSize = 100000, iterations = 10)
public class ArraySortBenchmark {
@State(Scope.Thread)
public static class Initialize {
String[] words = {"Java", "Baeldung", "Tutorial"};
List<String> wordList = new ArrayList<>();
@Setup(Level.Trial)
public void setUp() {
wordList.add("Java");
wordList.add("Baeldung");
wordList.add("Tutorial");
}
Integer[] numbers = {5, 22, 10, 0};
int[] primitives = {5, 22, 10, 0};
}
@Benchmark
public String[] benchmarkArraysSort(ArraySortBenchmark.Initialize state) {
Arrays.sort(state.words);
return state.words;
public Integer[] benchmarkArraysIntegerSort(ArraySortBenchmark.Initialize state) {
Arrays.sort(state.numbers);
return state.numbers;
}
@Benchmark
public List<String> benchmarkCollectionsSort(ArraySortBenchmark.Initialize state) {
Collections.sort(state.wordList);
return state.wordList;
public int[] benchmarkArraysIntSort(ArraySortBenchmark.Initialize state) {
Arrays.sort(state.primitives);
return state.primitives;
}
public static void main(String[] args) throws Exception {
Options options = new OptionsBuilder()
.include(ArraySortBenchmark.class.getSimpleName()).threads(1)