Merge pull request #7592 from dupirefr/bael-2996-ter

dupirefr/dupire.francois+pro@gmail.com [BAEL-2996] Using OptionsBuilder instead of main method for benchmarking
This commit is contained in:
Erik Pragt 2019-08-18 10:33:22 +10:00 committed by GitHub
commit 6bd963d9e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 154 additions and 52 deletions

View File

@ -0,0 +1,23 @@
package com.baeldung.matrices;
public class HomemadeMatrix {
public static double[][] multiplyMatrices(double[][] firstMatrix, double[][] secondMatrix) {
double[][] result = new double[firstMatrix.length][secondMatrix[0].length];
for (int row = 0; row < result.length; row++) {
for (int col = 0; col < result[row].length; col++) {
result[row][col] = multiplyMatricesCell(firstMatrix, secondMatrix, row, col);
}
}
return result;
}
private static double multiplyMatricesCell(double[][] firstMatrix, double[][] secondMatrix, int row, int col) {
double cell = 0;
for (int i = 0; i < secondMatrix.length; i++) {
cell += firstMatrix[row][i] * secondMatrix[i][col];
}
return cell;
}
}

View File

@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.Test;
public class CombinationUnitTest {
public class LogarithmUnitTest {
@Test
public void givenLog10_shouldReturnValidResults() {
@ -13,9 +13,9 @@ public class CombinationUnitTest {
}
@Test
public void givenLog10_shouldReturnValidResults() {
public void givenLogE_shouldReturnValidResults() {
assertEquals(Math.log(Math.E), 1);
assertEquals(Math.log(10), 2.30258);
assertEquals(Math.log(10), 2.30258, 0.00001);
}
@Test

View File

@ -1,9 +1,84 @@
package com.baeldung.matrices;
import cern.colt.matrix.DoubleFactory2D;
import cern.colt.matrix.DoubleMatrix2D;
import cern.colt.matrix.linalg.Algebra;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;
import org.ejml.simple.SimpleMatrix;
import org.la4j.Matrix;
import org.la4j.matrix.dense.Basic2DMatrix;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
public class MatrixMultiplicationBenchmarking {
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
Options opt = new OptionsBuilder()
.include(MatrixMultiplicationBenchmarking.class.getSimpleName())
.mode(Mode.AverageTime)
.forks(2)
.warmupIterations(5)
.measurementIterations(10)
.timeUnit(TimeUnit.MICROSECONDS)
.build();
new Runner(opt).run();
}
}
@Benchmark
public Object homemadeMatrixMultiplication(MatrixProvider matrixProvider) {
return HomemadeMatrix.multiplyMatrices(matrixProvider.getFirstMatrix(), matrixProvider.getSecondMatrix());
}
@Benchmark
public Object ejmlMatrixMultiplication(MatrixProvider matrixProvider) {
SimpleMatrix firstMatrix = new SimpleMatrix(matrixProvider.getFirstMatrix());
SimpleMatrix secondMatrix = new SimpleMatrix(matrixProvider.getSecondMatrix());
return firstMatrix.mult(secondMatrix);
}
@Benchmark
public Object apacheCommonsMatrixMultiplication(MatrixProvider matrixProvider) {
RealMatrix firstMatrix = new Array2DRowRealMatrix(matrixProvider.getFirstMatrix());
RealMatrix secondMatrix = new Array2DRowRealMatrix(matrixProvider.getSecondMatrix());
return firstMatrix.multiply(secondMatrix);
}
@Benchmark
public Object la4jMatrixMultiplication(MatrixProvider matrixProvider) {
Matrix firstMatrix = new Basic2DMatrix(matrixProvider.getFirstMatrix());
Matrix secondMatrix = new Basic2DMatrix(matrixProvider.getSecondMatrix());
return firstMatrix.multiply(secondMatrix);
}
@Benchmark
public Object nd4jMatrixMultiplication(MatrixProvider matrixProvider) {
INDArray firstMatrix = Nd4j.create(matrixProvider.getFirstMatrix());
INDArray secondMatrix = Nd4j.create(matrixProvider.getSecondMatrix());
return firstMatrix.mmul(secondMatrix);
}
@Benchmark
public Object coltMatrixMultiplication(MatrixProvider matrixProvider) {
DoubleFactory2D doubleFactory2D = DoubleFactory2D.dense;
DoubleMatrix2D firstMatrix = doubleFactory2D.make(matrixProvider.getFirstMatrix());
DoubleMatrix2D secondMatrix = doubleFactory2D.make(matrixProvider.getSecondMatrix());
Algebra algebra = new Algebra();
return algebra.mult(firstMatrix, secondMatrix);
}
}

View File

@ -0,0 +1,33 @@
package com.baeldung.matrices;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
@State(Scope.Benchmark)
public class MatrixProvider {
private double[][] firstMatrix;
private double[][] secondMatrix;
public MatrixProvider() {
firstMatrix =
new double[][] {
new double[] {1d, 5d},
new double[] {2d, 3d},
new double[] {1d ,7d}
};
secondMatrix =
new double[][] {
new double[] {1d, 2d, 3d, 7d},
new double[] {5d, 2d, 8d, 1d}
};
}
public double[][] getFirstMatrix() {
return firstMatrix;
}
public double[][] getSecondMatrix() {
return secondMatrix;
}
}

View File

@ -7,15 +7,10 @@ import org.openjdk.jmh.annotations.*;
import static org.assertj.core.api.Assertions.assertThat;
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 2)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
public class RealMatrixUnitTest {
class RealMatrixUnitTest {
@Test
@Benchmark
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
RealMatrix firstMatrix = new Array2DRowRealMatrix(
new double[][] {
new double[] {1d, 5d},
@ -43,5 +38,4 @@ public class RealMatrixUnitTest {
assertThat(actual).isEqualTo(expected);
}
}

View File

@ -8,15 +8,10 @@ import org.openjdk.jmh.annotations.*;
import static org.assertj.core.api.Assertions.assertThat;
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 2)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
public class DoubleMatrix2DUnitTest {
class DoubleMatrix2DUnitTest {
@Test
@Benchmark
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
DoubleFactory2D doubleFactory2D = DoubleFactory2D.dense;
DoubleMatrix2D firstMatrix = doubleFactory2D.make(
@ -48,4 +43,4 @@ public class DoubleMatrix2DUnitTest {
assertThat(actual).isEqualTo(expected);
}
}
}

View File

@ -6,15 +6,10 @@ import org.openjdk.jmh.annotations.*;
import static org.assertj.core.api.Assertions.assertThat;
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 2)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
public class SimpleMatrixUnitTest {
class SimpleMatrixUnitTest {
@Test
@Benchmark
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
SimpleMatrix firstMatrix = new SimpleMatrix(
new double[][] {
new double[] {1d, 5d},
@ -43,4 +38,4 @@ public class SimpleMatrixUnitTest {
assertThat(actual).matches(m -> m.isIdentical(expected, 0d));
}
}
}

View File

@ -5,15 +5,10 @@ import org.openjdk.jmh.annotations.*;
import static org.assertj.core.api.Assertions.assertThat;
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 2)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
public class HomemadeMatrixUnitTest {
class HomemadeMatrixUnitTest {
@Test
@Benchmark
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
double[][] firstMatrix = {
new double[]{1d, 5d},
new double[]{2d, 3d},
@ -55,4 +50,5 @@ public class HomemadeMatrixUnitTest {
}
return cell;
}
}
}

View File

@ -7,15 +7,10 @@ import org.openjdk.jmh.annotations.*;
import static org.assertj.core.api.Assertions.assertThat;
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 2)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
public class Basic2DMatrixUnitTest {
class Basic2DMatrixUnitTest {
@Test
@Benchmark
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
Matrix firstMatrix = new Basic2DMatrix(
new double[][]{
new double[]{1d, 5d},
@ -44,4 +39,4 @@ public class Basic2DMatrixUnitTest {
assertThat(actual).isEqualTo(expected);
}
}
}

View File

@ -7,14 +7,10 @@ import org.openjdk.jmh.annotations.*;
import static org.assertj.core.api.Assertions.assertThat;
@BenchmarkMode(Mode.AverageTime)
@Fork(value = 2)
@Warmup(iterations = 5)
@Measurement(iterations = 10)
public class INDArrayUnitTest {
class INDArrayUnitTest {
@Test
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
INDArray firstMatrix = Nd4j.create(
new double[][]{
new double[]{1d, 5d},
@ -43,4 +39,4 @@ public class INDArrayUnitTest {
assertThat(actual).isEqualTo(expected);
}
}
}