long before = Runtime.getRuntime().freeMemory();
long size = (((long)rows)*columns)*runs;
DoubleMatrix2D matrix = null;
if (kind.equals("sparse")) matrix = new SparseDoubleMatrix2D(rows,columns,initialCapacity,minLoadFactor,maxLoadFactor);
else if (kind.equals("dense")) matrix = new DenseDoubleMatrix2D(rows,columns);
//else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(rows,columns);
else throw new RuntimeException("unknown kind");
System.out.println("\nNow filling...");
//if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
for (int i=0; i<runs; i++) {
matrix.assign(0);
matrix.ensureCapacity(initialCapacity);
if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).ensureCapacity(initialCapacity);
timer1.start();
int value = 0;
for (int row=0; row < rows; row++) {
for (int column=0; column < columns; column++) {
matrix.setQuick(row,column,value++);
}
}
timer1.stop();
}
timer1.display();
timer1.minus(emptyLoop).display();
System.out.println(size / timer1.minus(emptyLoop).seconds() +" elements / sec");
Runtime.getRuntime().gc(); // invite gc
try { Thread.currentThread().sleep(1000); } catch (InterruptedException exc) {};
long after = Runtime.getRuntime().freeMemory();
System.out.println("KB needed="+(before-after) / 1024);
System.out.println("bytes needed per non-zero="+(before-after) / (double)matrix.cardinality());
if (print) {
System.out.println(matrix);
if (kind.equals("sparse")) System.out.println("map="+((SparseDoubleMatrix2D)matrix).elements);
}
/*
if (kind.equals("sparse")) {
int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
System.out.println("hashCollisions="+hashCollisions);
System.out.println("--> "+ ((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<runs; i++) {
for (int row=0; row < rows; row++) {
for (int column=0; column < columns; column++) {
element += matrix.getQuick(row,column);
}
}
}
timer2.stop().display();
timer2.minus(emptyLoop2).display();
System.out.println(size / timer2.minus(emptyLoop2).seconds() +" elements / sec");
if (print) System.out.println(matrix);
//if (kind.equals("sparse")) System.out.println("hashCollisions="+((SparseDoubleMatrix2D)matrix).elements.hashCollisions);
System.out.println(element); // !!! so that the jitter can't optimize away the whole loop
System.out.println("\nNow reading view...");
DoubleMatrix2D view = matrix.viewPart(0,0,rows,columns);
timer4.start();
element=0;
for (int i=0; i<runs; i++) {
for (int row=0; row < rows; row++) {
for (int column=0; column < columns; column++) {
element += view.getQuick(row,column);
}
}
}
timer4.stop().display();
timer4.minus(emptyLoop2).display();