<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/comparing-symmetric-eigenvalue-performance/"></div></div><div><a data-url="https://www.r-bloggers.com/comparing-symmetric-eigenvalue-performance/" data-counturl="https://www.r-bloggers.com/comparing-symmetric-eigenvalue-performance/" data-count="horizontal" data-text="Comparing Symmetric Eigenvalue Performance" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://wrathematics.github.io/2016/10/28/comparing-symmetric-eigenvalue-performance/"> wrathematics</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Lazy reader’s guide: skip to the pretty pictures, skim the conclusions section, ignore the rest.</p>
<h2>Background</h2>
<p>I think a lot about eigenvalue and singular value decompositions. I won’t get into it right now, but I have been quoted in the past as saying that singular values are eigenvalues for grownups. But today we need to go back to gradeschool to talk about singular values for babies: eigenvalues.</p>
<p>In R, most of your linear algebra is handled by the BLAS and LAPACK libraries. The BLAS handles really basic vector and matrix operations, while LAPACK has all the fun stuff like factorizations, including eigenvalues. This bit is important but has been written about ad infinitum, so I’ll keep it brief: use good BLAS. R ships with the Reference BLAS, which are slow. If you don’t know what any of this means, the easiest way forward is to just download <a target="_blank" rel="nofollow" href="https://mran.revolutionanalytics.com/download/">Microsoft R Open</a> (formerly from Revolution Analytics). It’s basically R compiled with Intel compilers and shipped with MKL (good BLAS, and more actually).</p>
<p>There are about a million ways to compute eigenvalues in LAPACK, but if your data looks like the way we store matrices in R, there are two significant ones: relatively robust representations (RRR) and divide and conquer (D&amp;C). If you search around, you will find people generally say that</p>
<ol>
<li>RRR is faster than D&amp;C</li>
<li>RRR uses less RAM than D&amp;C</li>
</ol>
<p>But I couldn’t really find much more detail than that. I’ve been doing some experiments of my own with computing eigenvalues in the <a target="_blank" rel="nofollow" href="https://github.com/wrathematics/band">band package</a>, which is a new framework for handling banded matrices in R. In the course of my experiments, I made a wrapper for the D&amp;C method, and ended up comparing it against R. My implementation was faster, by a margin that made me raise an eyebrow. Eventually I figured out that R uses the RRR method; but this seemed to run counter to the internet wisdom that D&amp;C is slower.</p>
<p>The great thing about doing computational work is that you don’t have to accept anybody else’s bullshit. We can test this, and I don’t need a team of researchers or grant money or IRB signatures or anything. We just need a free afternoon and a little elbow grease.</p>
<p></p>
<h2>The Benchmark</h2>
<p>The quick summary is that we’ll be measuring RRR versus D&amp;C wall clock run times with 5 replications with the following variations:</p>
<ol>
<li>9 different problem sizes (“n”), namely 25, 50, 100, 250, 500, 1000, 2000, 4000, and 8000.</li>
<li>4 different BLAS libraries: Reference, Atlas, OpenBLAS, and MKL.</li>
</ol>
<p>Hardware is a 4 core Intel Core i5-2500K CPU @ 3.30GHz. Additional software is as follows:</p>
<ul>
<li>R 3.1.1</li>
<li>gcc 6.2.0-5</li>
<li>Microsoft R Open 3.3.1 and Intel MKL</li>
<li>Whatever versions of Reference, Atlas, and OpenBLAS that are in Ubuntu 16.10 at the time of writing.</li>
</ul>
<p>Reference and Atlas timings are with one core; OpenBLAS and MKL are with 4.</p>
<p></p>
<h2>Results</h2>
<p>Displaying all of this is kind of difficult, but ggplot2 certainly does make the process less painful. And I’ve never claimed to be a vis expert; so I’d better not see a bunch of vis hipsters posting sassy bullshit on twitter over my plots.</p>
<p><img alt="" src="https://i0.wp.com/wrathematics.github.io/assets/2016-eigen/eig_bench.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>There are several things that are striking in this first plot:</p>
<ul>
<li>MKL is only slightly faster than OpenBLAS; both are much faster than Atlas and Reference BLAS (old news).</li>
<li>There’s very little variability in the run times, particularly for the larger problem sizes.</li>
<li>We still haven’t really answered our original question.</li>
</ul>
<p>To better address the RRR vs D&amp;C bit, let’s focus in only on the two largest problem sizes:</p>
<p><img alt="" src="https://i2.wp.com/wrathematics.github.io/assets/2016-eigen/eig_bench2.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>For both the 4000 and 8000 problem sizes, it’s pretty obvious that D&amp;C is faster with OpenBLAS and MKL, and slower with Reference and Atlas. So it feels like we have our answer. But feels can be deceiving.</p>
<p>Now after seeing this second plot, I was just about ready to call it a day. But then I started thinking; the two benchmarks where D&amp;C consistently won were OpenBLAS and MKL, which each were using 4 cores. By contrast, Atlas and the Reference BLAS (the two where D&amp;C lost) were strictly serial. What if these didn’t actually have <em>faster</em> D&amp;C implementations, but that D&amp;C was simply more scalable?</p>
<p>Turns out my hunch was correct:</p>
<p><img alt="" src="https://i2.wp.com/wrathematics.github.io/assets/2016-eigen/eig_bench3.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This run was with OpenBLAS only; we would almost certainly see the same results with MKL. But I also use this machine to watch Netflix, so I’m gonna stop heating up my house with benchmarks and go watch Star Trek.</p>
<p>So there. We settled it…</p>
<p></p>
<h2>But Wait! There’s More!</h2>
<p>We still haven’t looked at the memory allocation issue. This is actually much easier than you might expect if you’ve never worked with LAPACK before. The reason is, LAPACK was originally written in Fortran 77 — a language that does not support dynamic memory allocation. So any time a function (<code>subroutine</code> in Fortran) needed some local workspace, then <em>you</em> had to allocate it for that function. So all LAPACK functions are very up front in the documentation about allocation sizes. They even have a cute way of telling you the allocation requirements at run time.</p>
<p>Anyway, D&amp;C uses the <a target="_blank" rel="nofollow" href="http://www.netlib.org/lapack/double/dsyevd.f">dsyevd()</a> function, and RRR <a target="_blank" rel="nofollow" href="http://www.netlib.org/lapack/double/dsyevr.f">dsyevr()</a> (links are to original Fortran source code). You can skim the source to see if I’m lying to you, or take my word for it that the memory requirements are:</p>
<div>
<pre><code><span>library</span><span>(</span><span>memuse</span><span>)</span><span>
</span><span>dnc</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>n</span><span>)</span><span> </span><span>mu</span><span>(</span><span>1+6</span><span>*</span><span>n</span><span> </span><span>+</span><span> </span><span>2</span><span>*</span><span>n</span><span>*</span><span>n</span><span>)</span><span>*</span><span>8</span><span> </span><span>+</span><span> </span><span>mu</span><span>(</span><span>3</span><span> </span><span>+</span><span> </span><span>5</span><span>*</span><span>n</span><span>)</span><span>*</span><span>4</span><span>
</span><span>rrr</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>n</span><span>)</span><span> </span><span>mu</span><span>(</span><span>26</span><span>*</span><span>n</span><span>)</span><span>*</span><span>8</span><span> </span><span>+</span><span> </span><span>mu</span><span>(</span><span>10</span><span>*</span><span>n</span><span>)</span><span>*</span><span>4</span><span>
</span></code></pre>
</div>
<p>Now, if you’re willing to destroy the input data, then this calculation is a little unfair to D&amp;C; they get much closer in terms of the total amount of memory required to allocate to perform the calculation. But in R, we <em>can’t</em> destroy the input data, so for any R bindings, that is ultimately irrelevant. Also, the RRR version is a <em>lower bound</em> of the additional memory requirements. I went to the trouble to check what it’s actually using on my machine, and it’s never more than 2x the lower bound. You’re about to see why this factor of 2 is irrelevant.</p>
<p>Here are some figures for our <code>n=8000</code> problem size:</p>
<div>
<pre><code><span>n</span><span> </span><span>=</span><span> </span><span>8000</span><span>
</span><span># Size of an 8000x8000 matrix
</span><span>howbig</span><span>(</span><span>n</span><span>,</span><span> </span><span>n</span><span>)</span><span>
</span><span>## 488.281 MiB
</span><span>
</span><span># Extra memory use for D&amp;C method
</span><span>dnc</span><span>(</span><span>n</span><span>)</span><span>
</span><span>## 977.081 MiB
</span><span>
</span><span># Extra memory use for RRR method
</span><span>rrr</span><span>(</span><span>n</span><span>)</span><span>
</span><span>## 1.892 MiB
</span></code></pre>
</div>
<p>Pretty striking. If we had a problem size that pushed the RAM limits of most modern workstations, it doesn’t get any better:</p>
<div>
<pre><code><span>n</span><span> </span><span>=</span><span> </span><span>46340</span><span>
</span><span># Size of a 46340x46340 matrix
</span><span>howbig</span><span>(</span><span>n</span><span>,</span><span> </span><span>n</span><span>)</span><span>
</span><span>## 15.999 GiB
</span><span>
</span><span># Extra memory use for D&amp;C method
</span><span>dnc</span><span>(</span><span>n</span><span>)</span><span>
</span><span>## 32.002 GiB
</span><span>
</span><span># Extra memory use for RRR method
</span><span>rrr</span><span>(</span><span>n</span><span>)</span><span>
</span><span>## 10.960 MiB
</span></code></pre>
</div>
<p></p>
<h2>Conclusions</h2>
<ol>
<li>If you don’t know how to manage the BLAS that you’re using, for god’s sake just use the Revolution/Microsoft distribution of R.</li>
<li>RRR has better runtime performance than D&amp;C in serial, and worse in parallel.</li>
<li>D&amp;C gobbles up RAM at an astounding rate.</li>
<li>People who say that the D&amp;C method is slower are not quite right, or at least not completely right.</li>
<li>People who say that the D&amp;C method uses more RAM are underselling their case.</li>
<li>It would be really interesting to see how this scales with accelerator cards, like GPU’s and Intel MIC.</li>
</ol>
<p></p>
<h2>Data and R Scripts</h2>
<p>Here’s the data:</p>
<ul>
<li><a target="_blank" rel="nofollow" href="https://wrathematics.github.io/assets/2016-eigen/eig_bench.csv">varying problem size dataset</a></li>
<li><a target="_blank" rel="nofollow" href="https://wrathematics.github.io/assets/2016-eigen/eig_bench_8000.csv">varying cores OpenBLAS dataset</a></li>
</ul>
<p>And here’s the benchmarking script. I gave it the very descriptive name <code>x.r</code>:</p>
<div>
<pre><code><span>type</span><span> </span><span>=</span><span> </span><span>commandArgs</span><span>(</span><span>trailingOnly</span><span>=</span><span>TRUE</span><span>)</span><span>
</span><span>library</span><span>(</span><span>band</span><span>)</span><span>
</span><span>library</span><span>(</span><span>coop</span><span>)</span><span>
</span><span>data</span><span> </span><span>=</span><span> </span><span>function</span><span>(</span><span>n</span><span>)</span><span>
</span><span>{</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>rnorm</span><span>(</span><span>n</span><span>*</span><span>n</span><span>)</span><span>
</span><span>dim</span><span>(</span><span>x</span><span>)</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>n</span><span>,</span><span> </span><span>n</span><span>)</span><span>
</span><span>covar</span><span>(</span><span>x</span><span>)</span><span>
</span><span>}</span><span>
</span><span>nreps</span><span> </span><span>=</span><span> </span><span>5</span><span>
</span><span>N</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>25</span><span>,</span><span> </span><span>50</span><span>,</span><span> </span><span>100</span><span>,</span><span> </span><span>(</span><span>2</span><span>^</span><span>(</span><span>0</span><span>:</span><span>5</span><span>))</span><span>*</span><span>250</span><span>)</span><span>
</span><span>rowlen</span><span> </span><span>=</span><span> </span><span>length</span><span>(</span><span>N</span><span>)</span><span>*</span><span>nreps</span><span>
</span><span>df</span><span> </span><span>=</span><span> </span><span>data.frame</span><span>(</span><span>numeric</span><span>(</span><span>rowlen</span><span>),</span><span> </span><span>character</span><span>(</span><span>rowlen</span><span>),</span><span> </span><span>numeric</span><span>(</span><span>rowlen</span><span>),</span><span> </span><span>numeric</span><span>(</span><span>rowlen</span><span>),</span><span> </span><span>stringsAsFactors</span><span>=</span><span>FALSE</span><span>)</span><span>
</span><span>colnames</span><span>(</span><span>df</span><span>)</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;n&quot;</span><span>,</span><span> </span><span>&quot;type&quot;</span><span>,</span><span> </span><span>&quot;RRR&quot;</span><span>,</span><span> </span><span>&quot;D&amp;C&quot;</span><span>)</span><span>
</span><span>row</span><span> </span><span>=</span><span> </span><span>1L</span><span>
</span><span>for</span><span> </span><span>(</span><span>n</span><span> </span><span>in</span><span> </span><span>N</span><span>){</span><span>
</span><span>x</span><span> </span><span>=</span><span> </span><span>data</span><span>(</span><span>n</span><span>)</span><span>
</span><span>for</span><span> </span><span>(</span><span>rep</span><span> </span><span>in</span><span> </span><span>1</span><span>:</span><span>nreps</span><span>){</span><span>
</span><span>eigen_r</span><span> </span><span>=</span><span> </span><span>system.time</span><span>(</span><span>eigen</span><span>(</span><span>x</span><span>))[</span><span>3</span><span>]</span><span>
</span><span>eigen_me</span><span> </span><span>=</span><span> </span><span>system.time</span><span>(</span><span>eig</span><span>(</span><span>x</span><span>))[</span><span>3</span><span>]</span><span>
</span><span>df</span><span>[</span><span>row</span><span>,</span><span> </span><span>]</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>n</span><span>,</span><span> </span><span>type</span><span>,</span><span> </span><span>eigen_r</span><span>,</span><span> </span><span>eigen_me</span><span>)</span><span>
</span><span>row</span><span> </span><span>=</span><span> </span><span>row</span><span>+1L</span><span>
</span><span>}</span><span>
</span><span>}</span><span>
</span></code></pre>
</div>
<p>Invoke this via <code>Rscript x.r NameOfBLASLib</code>. So for example, if we were running with OpenBLAS, we’d use:</p>
<div>
<pre><code>Rscript x.r OpenBLAS
</code></pre>
</div>
<p>Now unfortunately, this naming scheme doesn’t actually <em>set</em> the BLAS library. It’s only for plotting later. I ran this on an Ubuntu machine, where changing BLAS is trivial. Simply run:</p>
<div>
<pre><code>sudo update-alternatives --config libblas.so.3
</code></pre>
</div>
<p>from the command line and choose the one you want. On other systems you have to swap things out manually, or possibly even rebuild R.</p>
<p>As for the plots, if you read in the <code>eig_bench.csv</code> data above to the R object <code>df</code>, then this is straightforward:</p>
<div>
<pre><code><span>library</span><span>(</span><span>reshape2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>plyr</span><span>)</span><span>
</span><span>dfm</span><span> </span><span>=</span><span> </span><span>melt</span><span>(</span><span>df</span><span>,</span><span> </span><span>id.vars</span><span>=</span><span>c</span><span>(</span><span>&quot;n&quot;</span><span>,</span><span> </span><span>&quot;type&quot;</span><span>))</span><span>
</span><span>se</span><span> </span><span>=</span><span> </span><span>ddply</span><span>(</span><span>dfm</span><span>,</span><span> </span><span>c</span><span>(</span><span>&quot;n&quot;</span><span>,</span><span> </span><span>&quot;type&quot;</span><span>,</span><span> </span><span>&quot;variable&quot;</span><span>),</span><span> </span><span>summarise</span><span>,</span><span> </span><span>mean</span><span>=</span><span>mean</span><span>(</span><span>value</span><span>),</span><span> </span><span>se</span><span>=</span><span>sd</span><span>(</span><span>value</span><span>)</span><span>/</span><span>sqrt</span><span>(</span><span>length</span><span>(</span><span>value</span><span>)))</span><span>
</span><span>se</span><span> </span><span>=</span><span> </span><span>transform</span><span>(</span><span>se</span><span>,</span><span> </span><span>lower</span><span>=</span><span>mean</span><span>-</span><span>se</span><span>,</span><span> </span><span>upper</span><span>=</span><span>mean</span><span>+</span><span>se</span><span>)</span><span>
</span><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>library</span><span>(</span><span>scales</span><span>)</span><span>
</span><span>ggplot</span><span>(</span><span>se</span><span>,</span><span> </span><span>aes</span><span>(</span><span>n</span><span>,</span><span> </span><span>mean</span><span>,</span><span> </span><span>fill</span><span>=</span><span>type</span><span>))</span><span> </span><span>+</span><span> </span><span>theme_bw</span><span>()</span><span> </span><span>+</span><span> </span><span>geom_bar</span><span>(</span><span>stat</span><span>=</span><span>&quot;identity&quot;</span><span>)</span><span> </span><span>+</span><span> </span><span>guides</span><span>(</span><span>fill</span><span>=</span><span>FALSE</span><span>)</span><span> </span><span>+</span><span>
</span><span>facet_grid</span><span>(</span><span>type</span><span> </span><span>~</span><span> </span><span>variable</span><span>)</span><span> </span><span>+</span><span> </span><span>xlab</span><span>(</span><span>&quot;Problem Size&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>ylab</span><span>(</span><span>&quot;Log10 Average Wall Clock Time (5 Runs)&quot;</span><span>)</span><span> </span><span>+</span><span>
</span><span>scale_y_log10</span><span>()</span><span> </span><span>+</span><span>
</span><span>geom_errorbar</span><span>(</span><span>data</span><span>=</span><span>se</span><span>,</span><span> </span><span>position</span><span>=</span><span>position_dodge</span><span>(</span><span>0.9</span><span>),</span><span> </span><span>aes</span><span>(</span><span>ymin</span><span>=</span><span>lower</span><span>,</span><span> </span><span>ymax</span><span>=</span><span>upper</span><span>))</span><span>
</span></code></pre>
</div>
<p>The other two plots are pretty obvious variations of this; subset for the second, use the other dataset for the third. Consider it an exercise.</p>
<p></p>
<h2>A Note About Benchmarking</h2>
<p>Choosing sizes for benchmarking is important. The problem sizes I chose aren’t arbitrary. Well…they’re kind of arbitrary, but not completely.</p>
<p>When you’re benchmarking, it’s very important to pick the right size of data. If you’re only interested in microsecond performance (say you need to execute something millions+ times per second), for one, stop using R immediately, you’ve made a terrible mistake. And two, you probably don’t care about large problem sizes. The same is true in reverse. If you only care about large problems, then who cares if it’s “slow” on small ones; you’re probably only thinking about something executing once.</p>
<p>So what if you care about <em>both</em>? A lot of people get this wrong for some reason, but the short version is that you should look at lots of varying problem sizes. In particular, you should make sure your data no longer comfortably fits in cache. If you haven’t measured the performance of your implementation at, say, 2x the size of your largest cache, then whatever claims you have about performance are complete bullshit. Period.</p>
<p>Fortunately, in R we can look up cache sizes very easily with the <a target="_blank" rel="nofollow" href="https://github.com/shinra-dev/memuse">memuse package</a>. This is the result from my machine (the one I ran the benchmarks on):</p>
<div>
<pre><code><span>memuse</span><span>::</span><span>Sys.cachesize</span><span>()</span><span>
</span><span>## L1I: 32.000 KiB ## L1D: 32.000 KiB ## L2: 256.000 KiB ## L3: 6.000 MiB </span></code></pre>
</div>
<p>My biggest cache is L3, at 6 megs. Some server hardware even has L4 cache these days! Anyway, always make sure that your data doesn’t fit in cache, because this can hide terrible performance patterns.</p>
<p>There’s a very simple test we can do to prove this. We’ll use compiled code by way of Rcpp, because I need to have more control over the demonstration than I can get with pure R code (R functions have a lot of overhead and variability that get in the way, but this is still valid for pure R code). We’re going to create a function that takes an integer input n and output an nxn matrix of all 1’s. And we’ll do this in two ways, first by iterating over the rows then columns of the matrix, and second by iterating columns first then rows. We’ll name them, oh I don’t know, <code>bad()</code> and <code>good()</code> respectively, for no particular reason. Also for no particular reason, you should know that matrices in R are column-major.</p>
<p>Anyway, here they are:</p>
<div>
<pre><code><span>#include &lt;Rcpp.h>
</span>
<span>// [[Rcpp::export]]
</span><span>Rcpp</span><span>::</span><span>NumericMatrix</span> <span>bad</span><span>(</span><span>const</span> <span>int</span> <span>n</span><span>)</span>
<span>{</span>
  <span>Rcpp</span><span>::</span><span>NumericMatrix</span> <span>x</span><span>(</span><span>n</span><span>,</span> <span>n</span><span>);</span>
  
  <span>for</span> <span>(</span><span>int</span> <span>i</span><span>=</span><span>0</span><span>;</span> <span>i</span><span>&lt;</span><span>n</span><span>;</span> <span>i</span><span>++</span><span>)</span>
    <span>for</span> <span>(</span><span>int</span> <span>j</span><span>=</span><span>0</span><span>;</span> <span>j</span><span>&lt;</span><span>n</span><span>;</span> <span>j</span><span>++</span><span>)</span>
      <span>x</span><span>(</span><span>i</span><span>,</span> <span>j</span><span>)</span> <span>=</span> <span>1.</span><span>;</span>
  
  <span>return</span> <span>x</span><span>;</span>
<span>}</span>

<span>// [[Rcpp::export]]
</span><span>Rcpp</span><span>::</span><span>NumericMatrix</span> <span>good</span><span>(</span><span>const</span> <span>int</span> <span>n</span><span>)</span>
<span>{</span>
  <span>Rcpp</span><span>::</span><span>NumericMatrix</span> <span>x</span><span>(</span><span>n</span><span>,</span> <span>n</span><span>);</span>
  
  <span>for</span> <span>(</span><span>int</span> <span>j</span><span>=</span><span>0</span><span>;</span> <span>j</span><span>&lt;</span><span>n</span><span>;</span> <span>j</span><span>++</span><span>)</span>
    <span>for</span> <span>(</span><span>int</span> <span>i</span><span>=</span><span>0</span><span>;</span> <span>i</span><span>&lt;</span><span>n</span><span>;</span> <span>i</span><span>++</span><span>)</span>
      <span>x</span><span>(</span><span>i</span><span>,</span> <span>j</span><span>)</span> <span>=</span> <span>1.</span><span>;</span>
  
  <span>return</span> <span>x</span><span>;</span>
<span>}</span>
</code></pre>
</div>
<p>Save them as <code>cache_eg.cpp</code> and run the following in R to build them:</p>
<div>
<pre><code><span>library</span><span>(</span><span>Rcpp</span><span>)</span><span>
</span><span>library</span><span>(</span><span>microbenchmark</span><span>)</span><span>
</span><span>library</span><span>(</span><span>memuse</span><span>)</span><span>
</span><span>sourceCpp</span><span>(</span><span>&quot;cache_eg.cpp&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>We can test on a small matrix:</p>
<div>
<pre><code><span>n</span><span> </span><span>=</span><span> </span><span>100L</span><span>
</span><span>howbig</span><span>(</span><span>n</span><span>,</span><span> </span><span>n</span><span>)</span><span>
</span><span>## 78.125 KiB
</span><span>
</span><span>mb</span><span> </span><span>=</span><span> </span><span>microbenchmark</span><span>(</span><span>bad</span><span>(</span><span>n</span><span>),</span><span> </span><span>good</span><span>(</span><span>n</span><span>))</span><span>
</span><span>boxplot</span><span>(</span><span>mb</span><span>)</span><span>
</span></code></pre>
</div>
<p>And these look pretty close; maybe we shouldn’t have called one of them <code>bad()</code> after all?</p>
<p><img alt="" src="https://i0.wp.com/wrathematics.github.io/assets/2016-eigen/mb_100.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Let’s try a larger data size…</p>
<div>
<pre><code><span>n</span><span> </span><span>=</span><span> </span><span>4000L</span><span>
</span><span>howbig</span><span>(</span><span>n</span><span>,</span><span> </span><span>n</span><span>)</span><span>
</span><span>## 122.070 MiB
</span><span>
</span><span>mb</span><span> </span><span>=</span><span> </span><span>microbenchmark</span><span>(</span><span>bad</span><span>(</span><span>n</span><span>),</span><span> </span><span>good</span><span>(</span><span>n</span><span>))</span><span>
</span><span>boxplot</span><span>(</span><span>mb</span><span>)</span><span>
</span></code></pre>
</div>
<p>HOLY SHIT WHAT HAPPENED?!</p>
<p><img alt="" src="https://i2.wp.com/wrathematics.github.io/assets/2016-eigen/mb_4000.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>This problem was deliberately cooked up to make this kind of comparison; we could actually make it worse by adding parallelism, but generally it won’t look this bad even if you screw your data accesses up. But if you’re only ever comparing two functions on teensy tiny data, you <em>can not possibly know</em> how well it will perform on datasets that don’t fit in cache.</p>
<p>To better understand why this is so important, here’s a <a target="_blank" rel="nofollow" href="http://www.overbyte.com.au/misc/Lesson3/CacheFun.html">fun little interactive visualization</a> showing the relative speeds in fetching data from RAM versus cache. You may also be interested in some of my older posts <a target="_blank" rel="nofollow" href="http://librestats.com/2015/02/12/cache-rules-everything-around-me/">Cache Rules Everything Around Me</a> and <a target="_blank" rel="nofollow" href="http://librestats.com/2014/07/23/advanced-r-profiling-with-pbdpapi/">Advanced R Profiling with pbdPAPI</a>.</p>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/comparing-symmetric-eigenvalue-performance/"></div></div><div><a data-url="https://www.r-bloggers.com/comparing-symmetric-eigenvalue-performance/" data-counturl="https://www.r-bloggers.com/comparing-symmetric-eigenvalue-performance/" data-count="vertical" data-text="Comparing Symmetric Eigenvalue Performance" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://wrathematics.github.io/2016/10/28/comparing-symmetric-eigenvalue-performance/"> wrathematics</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/1W4NDQuOqGs"></a></div>