<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/5-ways-to-measure-running-time-of-r-code/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.alexejgossmann.com/benchmarking_r/"> Alexej's blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>A reviewer asked me to report detailed running times for all (so many :scream:) performed computations in one of my papers, and so I spent a Saturday morning figuring out my favorite way to benchmark R code. This is a quick summary of the options I found to be available.</p>
<p>A quick online search revealed at least three R packages for benchmarking R code (<code>rbenchmark</code>, <code>microbenchmark</code>, and <code>tictoc</code>). Additionally, base R provides at least two methods to measure the running time of R code (<code>Sys.time</code> and <code>system.time</code>). In the following I briefly go through the syntax of using each of the five option, and present my conclusions at the end.</p>
<h3>1. Using <code>Sys.time</code></h3>
<p>The run time of a chunk of code can be measured by taking the difference between the time at the start and at the end of the code chunk. Simple yet flexible :sunglasses:.</p>
<div>
<pre><code><span>sleep_for_a_minute</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>()</span><span> </span><span>{</span><span> </span><span>Sys.sleep</span><span>(</span><span>60</span><span>)</span><span> </span><span>}</span><span>
</span><span>start_time</span><span> </span><span>&lt;-</span><span> </span><span>Sys.time</span><span>()</span><span>
</span><span>sleep_for_a_minute</span><span>()</span><span>
</span><span>end_time</span><span> </span><span>&lt;-</span><span> </span><span>Sys.time</span><span>()</span><span>
</span><span>end_time</span><span> </span><span>-</span><span> </span><span>start_time</span><span>
</span><span># Time difference of 1.000327 mins
</span></code></pre>
</div>
<h3>2. Library <code>tictoc</code></h3>
<p>The functions <code>tic</code> and <code>toc</code> are used in the same manner for benchmarking as the just demonstrated <code>Sys.time</code>. However <code>tictoc</code> adds a lot more convenience to the whole.</p>
<p>The most recent development<sup><a rel="nofollow" href="http://www.alexejgossmann.com/benchmarking_r/#fn:1" target="_blank">1</a></sup> version of <code>tictoc</code> can be installed from github:</p>
<div>
<pre><code><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;collectivemedia/tictoc&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>One can time a single code chunk:</p>
<div>
<pre><code><span>library</span><span>(</span><span>tictoc</span><span>)</span><span>
</span><span>tic</span><span>(</span><span>&quot;sleeping&quot;</span><span>)</span><span>
</span><span>print</span><span>(</span><span>&quot;falling asleep...&quot;</span><span>)</span><span>
</span><span>sleep_for_a_minute</span><span>()</span><span>
</span><span>print</span><span>(</span><span>&quot;...waking up&quot;</span><span>)</span><span>
</span><span>toc</span><span>()</span><span>
</span><span># [1] &quot;falling asleep...&quot;
# [1] &quot;...waking up&quot;
# sleeping: 60.026 sec elapsed
</span></code></pre>
</div>
<p>Or nest multiple timers:</p>
<div>
<pre><code><span>tic</span><span>(</span><span>&quot;total&quot;</span><span>)</span><span>
</span><span>tic</span><span>(</span><span>&quot;data generation&quot;</span><span>)</span><span>
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>rnorm</span><span>(</span><span>50000</span><span>*</span><span>1000</span><span>),</span><span> </span><span>50000</span><span>,</span><span> </span><span>1000</span><span>)</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>1000</span><span>,</span><span> </span><span>1000</span><span>)</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>runif</span><span>(</span><span>1</span><span>)</span><span> </span><span>+</span><span> </span><span>X</span><span> </span><span>%*%</span><span> </span><span>b</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>50000</span><span>)</span><span>
</span><span>toc</span><span>()</span><span>
</span><span>tic</span><span>(</span><span>&quot;model fitting&quot;</span><span>)</span><span>
</span><span>model</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>X</span><span>)</span><span>
</span><span>toc</span><span>()</span><span>
</span><span>toc</span><span>()</span><span>
</span><span># data generation: 3.792 sec elapsed
# model fitting: 39.278 sec elapsed
# total: 43.071 sec elapsed
</span></code></pre>
</div>
<h3>3. Using <code>system.time</code></h3>
<p>One can time the evaluation of an R expression using <code>system.time</code>. For example, we can use it to measure the execution time of the function <code>sleep_for_a_minute</code> (defined above) as follows.</p>
<div>
<pre><code><span>system.time</span><span>({</span><span> </span><span>sleep_for_a_minute</span><span>()</span><span> </span><span>})</span><span>
</span><span># user system elapsed
# 0.004 0.000 60.051
</span></code></pre>
</div>
<p><strong>But what exactly are the reported times <code>user</code>, <code>system</code>, and <code>elapsed</code>?</strong> :confused:</p>
<p>Well, clearly <code>elapsed</code> is the wall clock time taken to execute the function <code>sleep_for_a_minute</code>, plus some benchmarking code wrapping it (that’s why it took slightly more than a minute to run I guess).</p>
<p>As for <code>user</code> and <code>system</code> times, <a rel="nofollow" href="http://r.789695.n4.nabble.com/Meaning-of-proc-time-td2303263.html#a2306691" target="_blank">William Dunlap has posted a great explanation to the r-help mailing list</a>:</p>
<blockquote>
<p>“User CPU time” gives the CPU time spent by the current process (i.e., the current R session) and “system CPU time” gives the CPU time spent by the kernel (the operating system) on behalf of the current process. The operating system is used for things like opening files, doing input or output, starting other processes, and looking at the system clock: operations that involve resources that many processes must share. Different operating systems will have different things done by the operating system.</p>
</blockquote>
<p>:grinning:</p>
<h3>4. Library <code>rbenchmark</code></h3>
<p>The documentation to the function <code>benchmark</code> from the <code>rbenchmark</code> R package describes it as “a simple wrapper around <code>system.time</code>”. However it adds a lot of convenience compared to bare <code>system.time</code> calls. For example it requires just one <code>benchmark</code> call to time multiple replications of multiple expressions. Additionally the returned results are conveniently organized in a data frame.</p>
<p>I installed the development<sup><a rel="nofollow" href="http://www.alexejgossmann.com/benchmarking_r/#fn:1" target="_blank">1</a></sup> version of the <code>rbenchmark</code> package from github:</p>
<div>
<pre><code><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;eddelbuettel/rbenchmark&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>For example purposes, let’s compare the time required to compute linear regression coefficients using three alternative computational procedures:</p>
<ol>
<li><code>lm</code>,</li>
<li>the Moore-Penrose pseudoinverse,</li>
<li>the Moore-Penrose pseudoinverse but without explicit matrix inverses.</li>
</ol>
<div>
<pre><code><span>library</span><span>(</span><span>rbenchmark</span><span>)</span><span>
</span><span>benchmark</span><span>(</span><span>&quot;lm&quot;</span><span> </span><span>=</span><span> </span><span>{</span><span>
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>rnorm</span><span>(</span><span>1000</span><span>),</span><span> </span><span>100</span><span>,</span><span> </span><span>10</span><span>)</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>X</span><span> </span><span>%*%</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>10</span><span>,</span><span> </span><span>10</span><span>)</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>100</span><span>)</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>X</span><span> </span><span>+</span><span> </span><span>0</span><span>)</span><span>$</span><span>coef</span><span>
</span><span>},</span><span>
</span><span>&quot;pseudoinverse&quot;</span><span> </span><span>=</span><span> </span><span>{</span><span>
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>rnorm</span><span>(</span><span>1000</span><span>),</span><span> </span><span>100</span><span>,</span><span> </span><span>10</span><span>)</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>X</span><span> </span><span>%*%</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>10</span><span>,</span><span> </span><span>10</span><span>)</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>100</span><span>)</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>solve</span><span>(</span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>y</span><span>
</span><span>},</span><span>
</span><span>&quot;linear system&quot;</span><span> </span><span>=</span><span> </span><span>{</span><span>
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>rnorm</span><span>(</span><span>1000</span><span>),</span><span> </span><span>100</span><span>,</span><span> </span><span>10</span><span>)</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>X</span><span> </span><span>%*%</span><span> </span><span>sample</span><span>(</span><span>1</span><span>:</span><span>10</span><span>,</span><span> </span><span>10</span><span>)</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>100</span><span>)</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>solve</span><span>(</span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>X</span><span>,</span><span> </span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>y</span><span>)</span><span>
</span><span>},</span><span>
</span><span>replications</span><span> </span><span>=</span><span> </span><span>1000</span><span>,</span><span>
</span><span>columns</span><span> </span><span>=</span><span> </span><span>c</span><span>(</span><span>&quot;test&quot;</span><span>,</span><span> </span><span>&quot;replications&quot;</span><span>,</span><span> </span><span>&quot;elapsed&quot;</span><span>,</span><span>
</span><span>&quot;relative&quot;</span><span>,</span><span> </span><span>&quot;user.self&quot;</span><span>,</span><span> </span><span>&quot;sys.self&quot;</span><span>))</span><span>
</span><span># test replications elapsed relative user.self sys.self
# 3 linear system 1000 0.167 1.000 0.208 0.240
# 1 lm 1000 0.930 5.569 0.952 0.212
# 2 pseudoinverse 1000 0.240 1.437 0.332 0.612
</span></code></pre>
</div>
<p>Here, the meaning of <code>elapsed</code>, <code>user.self</code>, and <code>sys.self</code> is the same as described above in the section about <code>system.time</code>, and <code>relative</code> is simply the time ratio with the fastest test. Interestingly <code>lm</code> is by far the slowest here.</p>
<h3>5. Library <code>microbenchmark</code></h3>
<p>The most recent development version of <code>microbenchmark</code> can be installed from github:</p>
<div>
<pre><code><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;olafmersmann/microbenchmarkCore&quot;</span><span>)</span><span>
</span><span>devtools</span><span>::</span><span>install_github</span><span>(</span><span>&quot;olafmersmann/microbenchmark&quot;</span><span>)</span><span>
</span></code></pre>
</div>
<p>Much like <code>benchmark</code> from the package <code>rbenchmark</code>, the function <code>microbenchmark</code> can be used to compare running times of multiple R code chunks. But it offers a great deal of convenience and additional functionality.</p>
<p>I find that one particularly nice feature of <code>microbenchmark</code> is the ability to automatically check the results of the benchmarked expressions with a user-specified function. This is demonstrated below, where we again compare three methods computing the coefficient vector of a linear model.</p>
<div>
<pre><code><span>library</span><span>(</span><span>microbenchmark</span><span>)</span><span>
</span><span>set.seed</span><span>(</span><span>2017</span><span>)</span><span>
</span><span>n</span><span> </span><span>&lt;-</span><span> </span><span>10000</span><span>
</span><span>p</span><span> </span><span>&lt;-</span><span> </span><span>100</span><span>
</span><span>X</span><span> </span><span>&lt;-</span><span> </span><span>matrix</span><span>(</span><span>rnorm</span><span>(</span><span>n</span><span>*</span><span>p</span><span>),</span><span> </span><span>n</span><span>,</span><span> </span><span>p</span><span>)</span><span>
</span><span>y</span><span> </span><span>&lt;-</span><span> </span><span>X</span><span> </span><span>%*%</span><span> </span><span>rnorm</span><span>(</span><span>p</span><span>)</span><span> </span><span>+</span><span> </span><span>rnorm</span><span>(</span><span>100</span><span>)</span><span>
</span><span>check_for_equal_coefs</span><span> </span><span>&lt;-</span><span> </span><span>function</span><span>(</span><span>values</span><span>)</span><span> </span><span>{</span><span>
</span><span>tol</span><span> </span><span>&lt;-</span><span> </span><span>1e-12</span><span>
</span><span>max_error</span><span> </span><span>&lt;-</span><span> </span><span>max</span><span>(</span><span>c</span><span>(</span><span>abs</span><span>(</span><span>values</span><span>[[</span><span>1</span><span>]]</span><span> </span><span>-</span><span> </span><span>values</span><span>[[</span><span>2</span><span>]]),</span><span>
</span><span>abs</span><span>(</span><span>values</span><span>[[</span><span>2</span><span>]]</span><span> </span><span>-</span><span> </span><span>values</span><span>[[</span><span>3</span><span>]]),</span><span>
</span><span>abs</span><span>(</span><span>values</span><span>[[</span><span>1</span><span>]]</span><span> </span><span>-</span><span> </span><span>values</span><span>[[</span><span>3</span><span>]])))</span><span>
</span><span>max_error</span><span> </span><span></span><span> </span><span>tol</span><span>
</span><span>}</span><span>
</span><span>mbm</span><span> </span><span>&lt;-</span><span> </span><span>microbenchmark</span><span>(</span><span>&quot;lm&quot;</span><span> </span><span>=</span><span> </span><span>{</span><span> </span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>lm</span><span>(</span><span>y</span><span> </span><span>~</span><span> </span><span>X</span><span> </span><span>+</span><span> </span><span>0</span><span>)</span><span>$</span><span>coef</span><span> </span><span>},</span><span>
</span><span>&quot;pseudoinverse&quot;</span><span> </span><span>=</span><span> </span><span>{</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>solve</span><span>(</span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>y</span><span>
</span><span>},</span><span>
</span><span>&quot;linear system&quot;</span><span> </span><span>=</span><span> </span><span>{</span><span>
</span><span>b</span><span> </span><span>&lt;-</span><span> </span><span>solve</span><span>(</span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>X</span><span>,</span><span> </span><span>t</span><span>(</span><span>X</span><span>)</span><span> </span><span>%*%</span><span> </span><span>y</span><span>)</span><span>
</span><span>},</span><span>
</span><span>check</span><span> </span><span>=</span><span> </span><span>check_for_equal_coefs</span><span>)</span><span>
</span><span>mbm</span><span>
</span><span># Unit: milliseconds
# expr min lq mean median uq max neval cld
# lm 96.12717 124.43298 150.72674 135.12729 188.32154 236.4910 100 c
# pseudoinverse 26.61816 28.81151 53.32246 30.69587 80.61303 145.0489 100 b
# linear system 16.70331 18.58778 35.14599 19.48467 22.69537 138.6660 100 a
</span></code></pre>
</div>
<p>We used the function argument <code>check</code> to check for equality (up to a maximal error of <code>1e-12</code>) of the results returned by the three methods. If the results weren’t equal, <code>microbenchmark</code> would return an error message.</p>
<p>Another great feature is the integration with <code>ggplot2</code> for plotting <code>microbenchmark</code> results.</p>
<div>
<pre><code><span>library</span><span>(</span><span>ggplot2</span><span>)</span><span>
</span><span>autoplot</span><span>(</span><span>mbm</span><span>)</span><span>
</span></code></pre>
</div>
<p><img alt="Microbenchmark results plot" src="https://i2.wp.com/www.alexejgossmann.com/images/mbm.png?w=456" data-recalc-dims="1"></p>
<h3>Conclusion</h3>
<p>The given demonstration of the different benchmarking functions is surely not exhaustive. Nevertheless I made some conclusions for my personal benchmarking needs:</p>
<ul>
<li>The <code>Sys.time</code> approach as well as the <code>tictoc</code> package can be used for timing (potentially nested) steps of a complicated algorithm (that’s often my use case). However, <code>tictoc</code> is more convenient, and (most importantly) foolproof.</li>
<li>We saw that <code>microbenchmark</code> returns other types of measurements than <code>benchmark</code>, and I think that in most situations the <code>microbenchmark</code> measurements are of a higher practical significance :stuck_out_tongue:.</li>
<li>To my knowledge <code>microbenchmark</code> is the only benchmarking package that has visualizations built in :+1:.</li>
</ul>
<p>For these reasons I will go with <code>microbenchmark</code> and <code>tictoc</code>. :bowtie:</p>
<hr>
<div>
<ol>
<li>
<p>Though the repository does not seem to be very active. So the github version is probably no different from the stable release on CRAN. <a rel="nofollow" href="http://www.alexejgossmann.com/benchmarking_r/#fnref:1" target="_blank">↩</a> <a rel="nofollow" href="http://www.alexejgossmann.com/benchmarking_r/#fnref:1:1" target="_blank">↩<sup>2</sup></a></p>
</li>
</ol>
</div>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://www.alexejgossmann.com/benchmarking_r/"> Alexej's blog</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/fL_rj99fT3I" height="1"></a></div>