Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@orionll
Last active November 13, 2018 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orionll/595d10ff37ffe0d8c3824e734055cf00 to your computer and use it in GitHub Desktop.
Save orionll/595d10ff37ffe0d8c3824e734055cf00 to your computer and use it in GitHub Desktop.
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
Copy link

Maybe you should return array[0] as array.length is an invariant that hotspot could optimize (dead code elimination)

@orionll
Copy link
Author

orionll commented Nov 13, 2018

@bourgesl Thanks. Fixed.

@bourgesl
Copy link

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