1

It takes (on my MBP with 2.9 GHz Intel Core i7 and 16 GB memory) more than 20 seconds to get cross-product of a 40,000 x 1,000 matrix:

> system.time(a <- crossprod(matrix(pi,40000,1000)))
   user  system elapsed 
 23.808   0.139  24.001 

Is there any way ever to make it faster? Thanks for your help.

Sotos
  • 51,121
  • 6
  • 32
  • 66
chan1142
  • 609
  • 4
  • 13

1 Answers1

5

By changing your code? Basically no. crossprod directly calls compiled code, you'll be hard pressed to find any performance gains, and any you find will be small.

(Barring extenuating circumstances--how dense is your matrix? If you've got a lot of 0s, then using a sparse matrix may help. I'm also assuming your matrix isn't just a constant matrix as in your example.)

You could also use Microsoft's Revolution R Open which is compiled to use a different BLAS, or compile R yourself with a more optimized BLAS. This link has good details on that. For pure linear algebra manipulations, this could have a quite large effect. This site shows order of magnitude speed-ups for matrix multiplication, cholesky decomposition, etc.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • Thanks, @Gregor. And yes you are right, it's not a constant matrix. Sparse matrix certainly helps in a special case of mine. Thanks!! I will examine the links for other cases. – chan1142 Dec 12 '18 at 14:36
  • 2
    You don't need to use MRO or compile R yourself. Just symlink to the BLAS you want to use. See https://stackoverflow.com/questions/28179093/r-and-nvblas-dynlib-on-a-mac and https://cran.r-project.org/bin/macosx/RMacOSX-FAQ.html#Which-BLAS-is-used-and-how-can-it-be-changed_003f (but note that the path mentioned in the FAQ is wrong). – Ista Dec 12 '18 at 14:56
  • @Ista Super! Thanks. I'll absolutely give it a try. – chan1142 Dec 12 '18 at 15:14