Last active
November 13, 2018 12:47
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.openjdk.jmh.annotations.*; | |
import java.util.Arrays; | |
import java.util.Random; | |
import java.util.concurrent.TimeUnit; | |
@BenchmarkMode(Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MICROSECONDS) | |
@State(Scope.Thread) | |
public class Sort { | |
@Param({"10", "1000", "100000", "10000000" }) | |
int size; | |
@Param({"1", "2", "3"}) | |
int seed; | |
int[] array; | |
int[] arrayToSort; | |
@Setup | |
public void setUp() { | |
this.array = new int[this.size]; | |
final Random random = new Random(this.seed); | |
for (int i = 0; i < this.array.length; i++) { | |
this.array[i] = random.nextInt(); | |
} | |
} | |
@Benchmark | |
public int dualPivot() { | |
this.arrayToSort = this.array.clone(); | |
Arrays.sort(this.arrayToSort); | |
return this.arrayToSort[0]; | |
} | |
@Benchmark | |
public int radix() { | |
this.arrayToSort = this.array.clone(); | |
RadixSort.sort(this.arrayToSort); | |
return this.arrayToSort[0]; | |
} | |
} |
@bourgesl Thanks. Fixed.
Your test seems correct, I suppose arrayToSort (cloning) should stay out of the benchmark. How costly is it ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe you should return array[0] as array.length is an invariant that hotspot could optimize (dead code elimination)