Skip to content

Commit

Permalink
final sort tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MherBaghinyan committed Feb 19, 2019
1 parent 337a5df commit 1f7bff6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,30 @@
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)
Expand Down

0 comments on commit 1f7bff6

Please sign in to comment.