Last active
September 6, 2016 13:59
-
-
Save jackmott/11e4327c88f41aebbab650b1294b5c68 to your computer and use it in GitHub Desktop.
JavaBench
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.sample; | |
| import org.openjdk.jmh.annotations.Benchmark; | |
| import org.openjdk.jmh.annotations.Scope; | |
| import org.openjdk.jmh.annotations.State; | |
| import org.openjdk.jmh.annotations.Setup; | |
| import org.openjdk.jmh.annotations.BenchmarkMode; | |
| import org.openjdk.jmh.annotations.Mode; | |
| import org.openjdk.jmh.annotations.OutputTimeUnit; | |
| import org.openjdk.jmh.infra.Blackhole; | |
| import java.util.concurrent.TimeUnit; | |
| import java.util.Arrays; | |
| @State(Scope.Benchmark) | |
| public class MyBenchmark { | |
| double[] values; | |
| @Setup | |
| public void setup() | |
| { | |
| values = new double[1000000]; | |
| for (int i =0; i < values.length;i++) { | |
| values[i] = 2.0; | |
| } | |
| } | |
| @Benchmark | |
| @BenchmarkMode(Mode.AverageTime) | |
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | |
| public double scalar() { | |
| return Arrays.stream(values).reduce(0,(acc,x) -> Math.sin(x)); | |
| } | |
| @Benchmark | |
| @BenchmarkMode(Mode.AverageTime) | |
| @OutputTimeUnit(TimeUnit.MILLISECONDS) | |
| public double parallel() { | |
| return Arrays.stream(values).parallel().reduce(0,(acc,x) -> Math.sin(x)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment