dupirefr/dupire.francois+pro@gmail.com [BAEL-3606] Matrix Multiplication Libraries Comparison (#8298)
* Added benchmarking on larger matrices * [BAEL-3606] Moved benchmarking to production code * [BAEL-3606] Added minor fix
This commit is contained in:
@@ -1,84 +0,0 @@
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user