From 1f7bff62eb7f8239fd59835791c19c25a46e4832 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Tue, 19 Feb 2019 13:28:30 +0400 Subject: [PATCH] final sort tests --- .../java/sort/CollectionsSortCompare.java | 39 +++++++++++++++++++ .../performance/ArraySortBenchmark.java | 27 +++++-------- 2 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 core-java-collections/src/main/java/com/baeldung/java/sort/CollectionsSortCompare.java diff --git a/core-java-collections/src/main/java/com/baeldung/java/sort/CollectionsSortCompare.java b/core-java-collections/src/main/java/com/baeldung/java/sort/CollectionsSortCompare.java new file mode 100644 index 0000000000..1eff522877 --- /dev/null +++ b/core-java-collections/src/main/java/com/baeldung/java/sort/CollectionsSortCompare.java @@ -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 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)); + } +} diff --git a/core-java-collections/src/main/java/com/baeldung/performance/ArraySortBenchmark.java b/core-java-collections/src/main/java/com/baeldung/performance/ArraySortBenchmark.java index 14cef2406a..a1d40657d3 100644 --- a/core-java-collections/src/main/java/com/baeldung/performance/ArraySortBenchmark.java +++ b/core-java-collections/src/main/java/com/baeldung/performance/ArraySortBenchmark.java @@ -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 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 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)