Skip to content

Instantly share code, notes, and snippets.

@jerrinot
Created October 20, 2014 17:06
Show Gist options
  • Save jerrinot/9e46f798856fe5eafcda to your computer and use it in GitHub Desktop.
Save jerrinot/9e46f798856fe5eafcda to your computer and use it in GitHub Desktop.
package org.openjdk.jmh.samples;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
@State(Scope.Benchmark)
public class SortedTest {
private static final int NO_OF_ITEMS = 5_000_000;
private List<Integer> arrayListValues = new ArrayList<>(NO_OF_ITEMS);
private Integer[] boxedValues = new Integer[NO_OF_ITEMS];
private int[] primitiveValues = new int[NO_OF_ITEMS];
private int[] unsortedOrder = new int[NO_OF_ITEMS];
private int[] sortedOrder = new int[NO_OF_ITEMS];
private Random r;
@Setup
public void setUp() {
r = new Random();
for (int i = 0; i < NO_OF_ITEMS; i++) {
unsortedOrder[i] = i;
sortedOrder[i] = i;
arrayListValues.add(r.nextInt());
primitiveValues[i] = r.nextInt();
}
shuffleArray(unsortedOrder);
}
@Benchmark
public void testSortedPrimitive(Blackhole bh) {
for (int i : sortedOrder) {
bh.consume(primitiveValues[i]);
}
}
@Benchmark
public void testSortedBoxed(Blackhole bh) {
for (int i : sortedOrder) {
bh.consume(boxedValues[i]);
}
}
@Benchmark
public void testSortedArrayList(Blackhole bh) {
for (int i : sortedOrder) {
bh.consume(arrayListValues.get(i));
}
}
@Benchmark
public void testUnsortedPrimitive(Blackhole bh) {
for (int i : unsortedOrder) {
bh.consume(primitiveValues[i]);
}
}
@Benchmark
public void testUnsortedBoxed(Blackhole bh) {
for (int i : unsortedOrder) {
bh.consume(boxedValues[i]);
}
}
@Benchmark
public void testUnsortedArrayList(Blackhole bh) {
for (int i : unsortedOrder) {
bh.consume(arrayListValues.get(i));
}
}
public static void main(String[] args) throws Throwable {
Options opt = new OptionsBuilder()
.include(SortedTest.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(20)
.threads(1)
.forks(3)
.build();
new Runner(opt).run();
}
static void shuffleArray(int[] ar)
{
Random rnd = new Random();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
}
}
@jerrinot
Copy link
Author


Benchmark                                    Mode  Samples    Score  Score error  Units
o.o.j.s.SortedTest.testSortedArrayList      thrpt       60   75.660        0.591  ops/s
o.o.j.s.SortedTest.testSortedBoxed          thrpt       60   76.619        0.293  ops/s
o.o.j.s.SortedTest.testSortedPrimitive      thrpt       60  211.560        1.035  ops/s
o.o.j.s.SortedTest.testUnsortedArrayList    thrpt       60   76.560        0.487  ops/s
o.o.j.s.SortedTest.testUnsortedBoxed        thrpt       60   77.130        0.175  ops/s
o.o.j.s.SortedTest.testUnsortedPrimitive    thrpt       60   20.902        0.112  ops/s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment