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 index | Colt | Commons Math | Jama | |||||||
1 | 11.880748 | 24.455125 | 9.828977 | |||||||
2 | 11.874975 | 24.265102 | 9.848916 | |||||||
3 | 9.772616 | 14.374153 | 9.826572 | |||||||
4 | 9.759679 | 14.368105 | 2.655915 | |||||||
5 | 9.799622 | 15.238928 | 2.649129 | |||||||
6 | 9.780556 | 14.741863 | 2.668104 | |||||||
7 | 9.72831 | 15.509909 | 2.646811 | |||||||
8 | 9.79838 | 15.724348 | 2.646069 | |||||||
9 | 9.726143 | 15.988762 | 2.646052 | |||||||
10 | 9.784505 | 15.121782 | 2.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.