Skip to content

Instantly share code, notes, and snippets.

@mtopolnik
Created July 14, 2014 16:44
Show Gist options
  • Save mtopolnik/453729c02b47d9485128 to your computer and use it in GitHub Desktop.
Save mtopolnik/453729c02b47d9485128 to your computer and use it in GitHub Desktop.
package org.sample;
import static java.util.Arrays.setAll;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OperationsPerInvocation;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@BenchmarkMode(Mode.AverageTime)
@OperationsPerInvocation(1)
@Warmup(iterations = 10, time = 500, timeUnit=MILLISECONDS)
@Measurement(iterations = 20, time = 500, timeUnit=MILLISECONDS)
@State(Scope.Thread)
@Threads(1)
@Fork(1)
public class Measure
{
static final int[] ds = new int[1<<20];
static { setAll(ds, i->(int)(Math.random()*(1<<7))); }
int multiplier;
@Setup public void x() { multiplier = (int)(Math.random()*10); }
@GenerateMicroBenchmark public double sequential() {
return Arrays.stream(ds).map(d -> multiplier*5*d).sum();
}
@GenerateMicroBenchmark public double parallel() {
return Arrays.stream(ds).parallel().map(d -> multiplier*5*d).sum();
}
@GenerateMicroBenchmark public double unorderedParallel() {
return Arrays.stream(ds).unordered().parallel().map(d -> multiplier*5*d).sum();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment