title: Why is boosts matrix multiplication slower than mine?
body: <p>I have implemented one matrix multiplication with <code>boost::numeric::ublas::matrix</code> (see <a href="https://github.com/MartinThoma/matrix-multiplication/blob/master/C%2B%2B/library-boost.cpp" rel="noreferrer">my full, working boost code</a>)</p>
  
  <pre class="lang-c++ prettyprint-override"><code>Result result = read ();
  
  boost::numeric::ublas::matrix&lt;int&gt; C;
  C = boost::numeric::ublas::prod(result.A, result.B);
  </code></pre>
  
  <p>and another one with the standard algorithm (see <a href="https://github.com/MartinThoma/matrix-multiplication/blob/master/C%2B%2B/ikj-algorithm.cpp" rel="noreferrer">full standard code</a>):</p>
  
  <pre class="lang-c++ prettyprint-override"><code>vector&lt; vector&lt;int&gt; &gt; ijkalgorithm(vector&lt; vector&lt;int&gt; &gt; A, 
                                      vector&lt; vector&lt;int&gt; &gt; B) {
      int n = A.size();
  
      // initialise C with 0s
      vector&lt;int&gt; tmp(n, 0);
      vector&lt; vector&lt;int&gt; &gt; C(n, tmp);
  
      for (int i = 0; i &lt; n; i++) {
          for (int k = 0; k &lt; n; k++) {
              for (int j = 0; j &lt; n; j++) {
                  C[i][j] += A[i][k] * B[k][j];
              }
          }
      }
      return C;
  }
  </code></pre>
  
  <p>This is how I test the speed:</p>
  
  <pre><code>time boostImplementation.out &gt; boostResult.txt
  diff boostResult.txt correctResult.txt
  
  time simpleImplementation.out &gt; simpleResult.txt
  diff simpleResult.txt correctResult.txt
  </code></pre>
  
  <p>Both programs read a hard-coded textfile which contains two 2000 x 2000 matrices.
  Both programs were compiled with these flags:</p>
  
  <pre><code>g++ -std=c++98 -Wall -O3 -g $(PROBLEM).cpp -o $(PROBLEM).out -pedantic
  </code></pre>
  
  <p>I got <strong>15 seconds</strong> for my implementation and over <strong>4 minutes</strong> for the boost-implementation!</p>
  
  <p>edit: After compiling it with </p>
  
  <pre><code>g++ -std=c++98 -Wall -pedantic -O3 -D NDEBUG -DBOOST_UBLAS_NDEBUG library-boost.cpp -o library-boost.out
  </code></pre>
  
  <p>I got <strong>28.19 seconds</strong> for the ikj-algorithm and <strong>60.99 seconds</strong> for Boost. So Boost is still considerably slower.</p>
  
  <p>Why is boost so much slower than my implementation?</p>