double[][] Is Fine

In my previous post, I suggest that keeping a double[] performs better than keeping a double[][] if you do matrix multiplications and other operations.

This is actually not true. I benchmarked 3 libraries, Colt (uses double[]), Apache Commons Math (uses double[][]) and Jama (uses double[][] cleverly). At first it looks like Jama has a similar performance as Colt (they avoid [][] slow access by a clever algorithm). But once hotspot hits, the difference is crazy and Jama becomes the fastest (Far ahead).




JDK 1.6.0 Linux 1000x1000 matrix multiplication on Intel Q6600
loop indexColtCommons MathJama
111.88074824.4551259.828977
211.87497524.2651029.848916
39.77261614.3741539.826572
49.75967914.3681052.655915
59.79962215.2389282.649129
69.78055614.7418632.668104
79.7283115.5099092.646811
89.7983815.7243482.646069
99.72614315.9887622.646052
109.78450515.1217822.644572
We don't include matrix construction time, and fetching the result. Only the multiplication is taken into account.

The difference is less pronounced on smaller matrices, but still there. Jama looks very good in this simple test case. In more real scenarios, the difference is not so obvious. For example Commons Math SVD is faster than Jama one.

Comments

comments powered by Disqus