Skip to content

Instantly share code, notes, and snippets.

@relgames
Last active August 29, 2015 14:19
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 relgames/840d89280237acd4c385 to your computer and use it in GitHub Desktop.
Save relgames/840d89280237acd4c385 to your computer and use it in GitHub Desktop.
package org.sample;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class MyBenchmark {
@Param({"100", "1000", "10000", "10000000"})
public int size;
@Benchmark
public double[] randomParallel() {
double[] vector = new double[size];
Arrays.parallelSetAll(vector, i -> Math.random());
return vector;
}
@Benchmark
public double[] randomSequential() {
double[] vector = new double[size];
Arrays.setAll(vector, i -> Math.random());
return vector;
}
@Benchmark
public double[] threadLocalRandomParallel() {
double[] vector = new double[size];
Arrays.parallelSetAll(vector, i -> ThreadLocalRandom.current().nextDouble());
return vector;
}
@Benchmark
public double[] threadLocalRandomSequential() {
double[] vector = new double[size];
Arrays.setAll(vector, i -> ThreadLocalRandom.current().nextDouble());
return vector;
}
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(MyBenchmark.class.getSimpleName())
.build();
new Runner(opt).run();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment