Created
July 25, 2017 20:36
-
-
Save wind57/4755743a8875b9b60b6b15d75b3eb7dc 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.erabii.doublearray; | |
import java.util.AbstractMap; | |
import java.util.Arrays; | |
import java.util.concurrent.TimeUnit; | |
import java.util.stream.IntStream; | |
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.OutputTimeUnit; | |
import org.openjdk.jmh.annotations.Param; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.TearDown; | |
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(org.openjdk.jmh.annotations.Mode.AverageTime) | |
@OutputTimeUnit(TimeUnit.MILLISECONDS) | |
@Warmup(iterations = 2, time = 2, timeUnit = TimeUnit.SECONDS) | |
@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS) | |
public class DoubleArraySum { | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder().include(DoubleArraySum.class.getSimpleName()) | |
.jvmArgs("-ea", "-Xms10g", "-Xmx10g") | |
.shouldFailOnError(true) | |
.build(); | |
new Runner(opt).run(); | |
} | |
@State(Scope.Thread) | |
public static class Holder { | |
@Param({ "100", "500", "1000", "3000", "8000" }) | |
public int howManyEntries; | |
double[][] left = null; | |
double[][] right = null; | |
@Setup | |
public void setUp() { | |
left = new double[howManyEntries][howManyEntries]; | |
right = new double[howManyEntries][howManyEntries]; | |
for (int i = 0; i < howManyEntries; ++i) { | |
for (int j = 0; j < howManyEntries; ++j) { | |
left[i][j] = j; | |
right[i][j] = j; | |
} | |
} | |
} | |
@TearDown | |
public void tearDown() { | |
left = null; | |
right = null; | |
} | |
} | |
@Fork(1) | |
@Benchmark | |
public double[][] federico1(Holder holder) { | |
double res[][] = new double[holder.howManyEntries][holder.howManyEntries]; | |
Arrays.parallelSetAll(res, i -> { | |
Arrays.parallelSetAll(res[i], j -> holder.left[i][j] + holder.right[i][j]); | |
return res[i]; | |
}); | |
return res; | |
} | |
@Fork(1) | |
@Benchmark | |
public double[][] federico2(Holder holder) { | |
double res[][] = new double[holder.howManyEntries][holder.howManyEntries]; | |
Arrays.parallelSetAll(res, i -> { | |
Arrays.setAll(res[i], j -> holder.left[i][j] + holder.right[i][j]); | |
return res[i]; | |
}); | |
return res; | |
} | |
@Fork(1) | |
@Benchmark | |
public double[][] dkatzel(Holder holder) { | |
double res[][] = new double[holder.howManyEntries][holder.howManyEntries]; | |
IntStream.range(0, holder.howManyEntries * holder.howManyEntries) | |
.parallel() | |
.forEach(i -> { | |
int x = i / holder.howManyEntries; | |
int y = i % holder.howManyEntries; | |
res[x][y] = holder.left[x][y] + holder.right[x][y]; | |
}); | |
return res; | |
} | |
@Fork(1) | |
@Benchmark | |
public double[][] holijava(Holder holder) { | |
double[][] res = IntStream.range(0, holder.left.length).parallel() | |
.mapToObj(i -> IntStream.range(0, holder.left[i].length) | |
.mapToDouble(j -> holder.left[i][j] + holder.right[i][j]) | |
.toArray()) | |
.toArray(double[][]::new); | |
return res; | |
} | |
@Fork(1) | |
@Benchmark | |
public double[][] eugene(Holder holder) { | |
double res[][] = new double[holder.howManyEntries][holder.howManyEntries]; | |
IntStream.range(0, holder.left.length) | |
.forEach(i -> Arrays.parallelSetAll(res[i], j -> holder.left[i][j] + holder.right[i][j])); | |
return res; | |
} | |
@Fork(1) | |
@Benchmark | |
public double[][] pivovarit(Holder holder) { | |
double res[][] = new double[holder.howManyEntries][holder.howManyEntries]; | |
IntStream.range(0, holder.left.length).boxed() | |
.flatMap(i -> IntStream.range(0, holder.left[0].length) | |
.mapToObj(j -> new AbstractMap.SimpleImmutableEntry<>(i, j))) | |
.parallel() | |
.forEach(e -> { | |
res[e.getKey()][e.getValue()] = holder.left[e.getKey()][e.getValue()] + holder.right[e.getKey()][e.getValue()]; | |
}); | |
return res; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment