cern.colt.matrix.impl.BenchmarkMatrix2D Maven / Gradle / Ivy
/*
Copyright (c) 1999 CERN - European Organization for Nuclear Research.
Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose
is hereby granted without fee, provided that the above copyright notice appear in all copies and
that both that copyright notice and this permission notice appear in supporting documentation.
CERN makes no representations about the suitability of this software for any purpose.
It is provided "as is" without expressed or implied warranty.
*/
package cern.colt.matrix.impl;
import cern.colt.matrix.DoubleMatrix2D;
/**
Benchmarks the performance of matrices. Here are the results of some encouraging
measurements. Note that all benchmarks only measure the time spent in accessing
a matrix element; they exclude the loop itself.
Iteration Performance [million method
calls per second]
Pentium Pro 200 Mhz, SunJDK 1.2.2, NT, java -classic,
60 times repeating the same iteration
Element type
Matrix2D type
.
DenseDoubleMatrix2D
1000 x 1000
SparseDoubleMatrix2D
100 x 1000,
minLoadFactor=0.2, maxLoadFactor=0.5, initialCapacity
= 0
getQuick
setQuick
getQuick
setQuick
double
5
5
1
0.27
int
5
5.5
1
0.3
As can be seen, sparse matrices are certainly not quite as quick
as dense ones, but not really slow either. Considering their minimal footprint
they can be a real alternative.
Comparing the OO abstractions to zero-abstraction primitive Java arrays may
or may not be useful. Still, the table below provides some interesting information.
For example, access to Type_T_Matrix2D is quicker than naive usage
of Type_T_[]. Primitive arrays should only be considered if the optimized
form can be applied. Note again that all benchmarks only measure the time spent
in accessing a matrix element; they exclude the loop itself.
Iteration Performance
[million element accesses per second]
Pentium Pro 200 Mhz, SunJDK 1.2.2, NT, java -classic,
200 times repeating the same iteration
Element type
Matrix2D type = Java array double[][]
.
Unoptimized Form
1000 x 1000
for (int row=0; row < rows; row++) {
for (int col=0; col < columns; ) {
value = m[row][col++];
...
}
}
Optimized Form
1000 x 1000
for (int row=0; row < rows; row++) {
int[] r = matrix[row];
for (int col=0; col < columns; ) {
value = r[col++];
...
}
}
getting
setting
getting
setting
double
1.6
1.8
18
11
int
1.5
1.8
28
26
@author [email protected]
@version 1.0, 09/24/99
*/
class BenchmarkMatrix2D {
/**
* Makes this class non instantiable, but still let's others inherit from it.
*/
protected BenchmarkMatrix2D() {
throw new RuntimeException("Non instantiable");
}
/**
* Runs a bench on matrices holding double elements.
*/
public static void doubleBenchmark(int runs, int rows, int columns, String kind, boolean print, int initialCapacity, double minLoadFactor, double maxLoadFactor) {
System.out.println("benchmarking double matrix");
// certain loops need to be constructed so that the jitter can't optimize them away and we get fantastic numbers.
// this involves primarly read-loops
cern.colt.Timer timer1 = new cern.colt.Timer();
cern.colt.Timer timer2 = new cern.colt.Timer();
cern.colt.Timer timer3 = new cern.colt.Timer();
cern.colt.Timer timer4 = new cern.colt.Timer();
cern.colt.Timer emptyLoop = new cern.colt.Timer();
cern.colt.Timer emptyLoop2 = new cern.colt.Timer();
emptyLoop.start();
int dummy = 0;
for (int i=0; i "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
}
*/
System.out.println("\nNow reading...");
//if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
timer2.start();
double element=0;
for (int i=0; i "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
}
*/
System.out.println("\nNow multiplying2...");
matrix.assign(1);
//if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
for (int i=0; i "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
}
*/
System.out.println("bye bye.");
}
/**
* Runs a bench on matrices holding double elements.
*/
public static void doubleBenchmarkPrimitive(int runs, int rows, int columns, boolean print) {
// certain loops need to be constructed so that the jitter can't optimize them away and we get fantastic numbers.
// this involves primarly read-loops
cern.colt.Timer timer1 = new cern.colt.Timer();
cern.colt.Timer timer2 = new cern.colt.Timer();
cern.colt.Timer timer3 = new cern.colt.Timer();
cern.colt.Timer emptyLoop = new cern.colt.Timer();
cern.colt.Timer emptyLoop2 = new cern.colt.Timer();
emptyLoop.start();
int dummy = 0;
for (int i=0; i "+ ((double)hashCollisions / (rows*columns)) +" probes/element on average.");
}
System.out.println("\nNow reading...");
if (kind.equals("sparse")) ((SparseIntMatrix2D)matrix).elements.hashCollisions = 0;
timer2.start();
int element=0;
for (int i=0; i