Skip to content

Instantly share code, notes, and snippets.

@jerrinot
Created October 21, 2014 09:27
Show Gist options
  • Save jerrinot/fe2f32a67d0d01a4c00a to your computer and use it in GitHub Desktop.
Save jerrinot/fe2f32a67d0d01a4c00a 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.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;
int value = r.nextInt();
arrayListValues.add(value);
primitiveValues[i] = value;
boxedValues[i] = value;
}
shuffleArray(unsortedOrder);
}
@Benchmark
public int testSortedPrimitive() {
int res = 0;
for (int i : sortedOrder) {
res += primitiveValues[i];
}
return res;
}
@Benchmark
public int testSortedBoxed() {
int res = 0;
for (int i : sortedOrder) {
res += boxedValues[i];
}
return res;
}
@Benchmark
public int testSortedArrayList() {
int res = 0;
for (int i : sortedOrder) {
res += arrayListValues.get(i);
}
return res;
}
@Benchmark
public int testUnsortedPrimitive() {
int res = 0;
for (int i : unsortedOrder) {
res += primitiveValues[i];
}
return res;
}
@Benchmark
public int testUnsortedBoxed() {
int res = 0;
for (int i : unsortedOrder) {
res += boxedValues[i];
}
return res;
}
@Benchmark
public int testUnsortedArrayList() {
int res = 0;
for (int i : unsortedOrder) {
res += arrayListValues.get(i);
}
return res;
}
public static void main(String[] args) throws Throwable {
Options opt = new OptionsBuilder()
.include(SortedTest.class.getSimpleName())
.warmupIterations(10)
.measurementIterations(20)
.threads(1)
.forks(2)
.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       40  105.613        5.343  ops/s
o.o.j.s.SortedTest.testSortedBoxed          thrpt       40  107.609        4.459  ops/s
o.o.j.s.SortedTest.testSortedPrimitive      thrpt       40  358.130       21.637  ops/s
o.o.j.s.SortedTest.testUnsortedArrayList    thrpt       40    9.875        0.203  ops/s
o.o.j.s.SortedTest.testUnsortedBoxed        thrpt       40   10.107        0.287  ops/s
o.o.j.s.SortedTest.testUnsortedPrimitive    thrpt       40   22.477        0.505  ops/s

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