Created
October 20, 2014 15:59
-
-
Save jerrinot/14290329cf039e3fbae4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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.Random; | |
@State(Scope.Benchmark) | |
public class SortedTest { | |
private static final int NO_OF_ITEMS = 5_000_000; | |
private int[] values = 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; | |
values[i] = r.nextInt(); | |
} | |
shuffleArray(unsortedOrder); | |
} | |
@Benchmark | |
public void testSorted(Blackhole bh) { | |
for (int i : sortedOrder) { | |
bh.consume(values[i]); | |
} | |
} | |
@Benchmark | |
public void testUnsorted(Blackhole bh) { | |
for (int i : unsortedOrder) { | |
bh.consume(values[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) | |
.syncIterations(true) // try to switch to "false" | |
.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; | |
} | |
} | |
} |
Author
jerrinot
commented
Oct 20, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment