title: How to speed up Eigen library's matrix product?
body: <p>I'm studying simple multiplication of two big matrices using the Eigen library. This multiplication appears to be noticeably slower than both Matlab and Python for the same size matrices. </p>
  
  <p>Is there anything to be done to make the Eigen operation faster?</p>
  
  <p><strong>Problem Details</strong></p>
  
  <p>X : random 1000 x 50000 matrix</p>
  
  <p>Y : random 50000 x 300 matrix</p>
  
  <p><strong>Timing experiments (on my late 2011 Macbook Pro)</strong></p>
  
  <p>Using Matlab:  X*Y takes ~1.3 sec</p>
  
  <p>Using Enthought Python:  numpy.dot( X, Y) takes ~ 2.2 sec</p>
  
  <p>Using Eigen:   X*Y takes ~2.7 sec</p>
  
  <p><strong>Eigen Details</strong></p>
  
  <p>You can get my Eigen code (as a MEX function): <a href="https://gist.github.com/michaelchughes/4742878" rel="noreferrer">https://gist.github.com/michaelchughes/4742878</a></p>
  
  <p>This MEX function reads in two matrices from Matlab, and returns their product.</p>
  
  <p>Running this MEX function without the matrix product operation (ie just doing the IO) produces negligible overhead, so the IO between the function and Matlab doesn't explain the big difference in performance. It's clearly the actual matrix product operation.</p>
  
  <p>I'm compiling with g++, with these optimization flags: "-O3 -DNDEBUG"</p>
  
  <p>I'm using the latest stable Eigen header files (3.1.2).</p>
  
  <p>Any suggestions on how to improve Eigen's performance? Can anybody replicate the gap I'm seeing?</p>
  
  <p><strong>UPDATE</strong>
  The compiler really seems to matter.  The original Eigen timing was done using Apple XCode's version of g++: llvm-g++-4.2.   </p>
  
  <p>When I use g++-4.7 downloaded via MacPorts (same CXXOPTIMFLAGS), I get 2.4 sec instead of 2.7.  </p>
  
  <p>Any other suggestions of how to compile better would be much appreciated.</p>
  
  <p>You can also get raw C++ code for this experiment: <a href="https://gist.github.com/michaelchughes/4747789" rel="noreferrer">https://gist.github.com/michaelchughes/4747789</a></p>
  
  <p>./MatProdEigen 1000 50000 300</p>
  
  <p>reports 2.4 seconds under g++-4.7</p>