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:
commit
6bd963d9e8
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,7 +4,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class CombinationUnitTest {
|
public class LogarithmUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenLog10_shouldReturnValidResults() {
|
public void givenLog10_shouldReturnValidResults() {
|
||||||
|
@ -13,9 +13,9 @@ public class CombinationUnitTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenLog10_shouldReturnValidResults() {
|
public void givenLogE_shouldReturnValidResults() {
|
||||||
assertEquals(Math.log(Math.E), 1);
|
assertEquals(Math.log(Math.E), 1);
|
||||||
assertEquals(Math.log(10), 2.30258);
|
assertEquals(Math.log(10), 2.30258, 0.00001);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
@ -1,9 +1,84 @@
|
||||||
package com.baeldung.matrices;
|
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 class MatrixMultiplicationBenchmarking {
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,15 +7,10 @@ import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
class RealMatrixUnitTest {
|
||||||
@Fork(value = 2)
|
|
||||||
@Warmup(iterations = 5)
|
|
||||||
@Measurement(iterations = 10)
|
|
||||||
public class RealMatrixUnitTest {
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Benchmark
|
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
||||||
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
|
||||||
RealMatrix firstMatrix = new Array2DRowRealMatrix(
|
RealMatrix firstMatrix = new Array2DRowRealMatrix(
|
||||||
new double[][] {
|
new double[][] {
|
||||||
new double[] {1d, 5d},
|
new double[] {1d, 5d},
|
||||||
|
@ -43,5 +38,4 @@ public class RealMatrixUnitTest {
|
||||||
|
|
||||||
assertThat(actual).isEqualTo(expected);
|
assertThat(actual).isEqualTo(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,15 +8,10 @@ import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
class DoubleMatrix2DUnitTest {
|
||||||
@Fork(value = 2)
|
|
||||||
@Warmup(iterations = 5)
|
|
||||||
@Measurement(iterations = 10)
|
|
||||||
public class DoubleMatrix2DUnitTest {
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Benchmark
|
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
||||||
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
|
||||||
DoubleFactory2D doubleFactory2D = DoubleFactory2D.dense;
|
DoubleFactory2D doubleFactory2D = DoubleFactory2D.dense;
|
||||||
|
|
||||||
DoubleMatrix2D firstMatrix = doubleFactory2D.make(
|
DoubleMatrix2D firstMatrix = doubleFactory2D.make(
|
||||||
|
|
|
@ -6,15 +6,10 @@ import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
class SimpleMatrixUnitTest {
|
||||||
@Fork(value = 2)
|
|
||||||
@Warmup(iterations = 5)
|
|
||||||
@Measurement(iterations = 10)
|
|
||||||
public class SimpleMatrixUnitTest {
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Benchmark
|
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
||||||
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
|
||||||
SimpleMatrix firstMatrix = new SimpleMatrix(
|
SimpleMatrix firstMatrix = new SimpleMatrix(
|
||||||
new double[][] {
|
new double[][] {
|
||||||
new double[] {1d, 5d},
|
new double[] {1d, 5d},
|
||||||
|
|
|
@ -5,15 +5,10 @@ import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
class HomemadeMatrixUnitTest {
|
||||||
@Fork(value = 2)
|
|
||||||
@Warmup(iterations = 5)
|
|
||||||
@Measurement(iterations = 10)
|
|
||||||
public class HomemadeMatrixUnitTest {
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Benchmark
|
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
||||||
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
|
||||||
double[][] firstMatrix = {
|
double[][] firstMatrix = {
|
||||||
new double[]{1d, 5d},
|
new double[]{1d, 5d},
|
||||||
new double[]{2d, 3d},
|
new double[]{2d, 3d},
|
||||||
|
@ -55,4 +50,5 @@ public class HomemadeMatrixUnitTest {
|
||||||
}
|
}
|
||||||
return cell;
|
return cell;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -7,15 +7,10 @@ import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
class Basic2DMatrixUnitTest {
|
||||||
@Fork(value = 2)
|
|
||||||
@Warmup(iterations = 5)
|
|
||||||
@Measurement(iterations = 10)
|
|
||||||
public class Basic2DMatrixUnitTest {
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Benchmark
|
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
||||||
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
|
||||||
Matrix firstMatrix = new Basic2DMatrix(
|
Matrix firstMatrix = new Basic2DMatrix(
|
||||||
new double[][]{
|
new double[][]{
|
||||||
new double[]{1d, 5d},
|
new double[]{1d, 5d},
|
||||||
|
|
|
@ -7,14 +7,10 @@ import org.openjdk.jmh.annotations.*;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@BenchmarkMode(Mode.AverageTime)
|
class INDArrayUnitTest {
|
||||||
@Fork(value = 2)
|
|
||||||
@Warmup(iterations = 5)
|
|
||||||
@Measurement(iterations = 10)
|
|
||||||
public class INDArrayUnitTest {
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
void givenTwoMatrices_whenMultiply_thenMultiplicatedMatrix() {
|
||||||
INDArray firstMatrix = Nd4j.create(
|
INDArray firstMatrix = Nd4j.create(
|
||||||
new double[][]{
|
new double[][]{
|
||||||
new double[]{1d, 5d},
|
new double[]{1d, 5d},
|
||||||
|
|
Loading…
Reference in New Issue