title: Why is matrix multiplication in .NET so slow?
body: <p>I don't quite understand what makes matrix multiplication in C#/.NET (and even Java) so slow.</p>
  
  <p><strike>Take a look at this benchmark (<a href="https://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html" rel="nofollow noreferrer">source</a>):</strike> Trying to find an updated benchmark.</p>
  
  <p><img src="https://web.archive.org/web/20130606093053/http://img411.imageshack.us/img411/9324/perf.gif" alt="Java vs C# vs C++ breakdown"></p>
  
  <p>C#'s integer and double performance is damn close to C++ compiled with MSVC++. 87% as fast for double and 99% as fast for 32-bit integer. Pretty damn good, I'd say. But then look at matrix multiplication. The gap widens to C# being about 19% as fast. This is a pretty huge discrepancy that I don't understand. Matrix multiplication is just a bunch of simple math. How is it getting so slow? Shouldn't it be roughly as fast as an equivalent number of simple floating point or integer operations?</p>
  
  <p>This is especially of a concern with games and with XNA, where matrix and vector performance are critical for things like physics engines. Some time ago, Mono added support for SIMD instructions through some nifty vector and matrix classes. It closes the gap and makes Mono faster than hand-written C++, although not as fast as C++ with SIMD. (<a href="https://stackoverflow.com/questions/2348967/c-c-versus-java-c-in-high-performance-applications">source</a>)</p>
  
  <p><a href="https://i.stack.imgur.com/130dt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/130dt.png" alt="Matrix multiplication comparison"></a>
  </p>
  
  <p>What's going on here?</p>
  
  <p>Edit: Looking closer, I misread the second graph. C# appears pretty close. <strike>Is the first benchmark just doing something horribly wrong?</strike> Sorry, I missed the version number on the first benchmark. I grabbed it as a handy reference for the "C# linear algebra is slow" that I've always heard. I'll try to find another.</p>