<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/timing-in-r/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/"> R on The Jumping Rivers Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<ul>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/#nested-timings" target="_blank">Nested timings</a>
<ul>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/#sys.time" target="_blank">1) <code>Sys.time()</code></a></li>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/#the-tictoc-package" target="_blank">2) The tictoc package</a></li>
</ul>
</li>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/#comparing-functions" target="_blank">Comparing functions</a>
<ul>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/#system.time" target="_blank">1) <code>system.time()</code></a></li>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/#the-microbenchmark-package" target="_blank">2) The microbenchmark package</a></li>
</ul>
</li>
<li><a rel="nofollow" href="http://blog.jumpingrivers.com/posts/2017/timing-in-r/#conclusion" target="_blank">Conclusion</a></li>
</ul>
</div>
<p>As time goes on, your R scripts are probably getting longer and more complicated, <em>right</em>? Timing parts of your script could save you precious time when re-running code over and over again. Today I’m going to go through the 4 main functions for doing so.</p>
<p><img src="https://i1.wp.com/blog.jumpingrivers.com/img/2017/front_timing.png?w=300" data-recalc-dims="1"></p>
<hr>
<div>
<h2>Nested timings</h2>
<div>
<h3>1) <code>Sys.time()</code></h3>
<p><code>Sys.time()</code> takes a “snap-shot” of the current time and so it can be used to record start and end times of code.</p>
<pre><code>start_time = Sys.time()
Sys.sleep(0.5)
end_time = Sys.time()</code></pre>
<p>To calculate the difference, we just use a simple subtraction</p>
<pre><code>end_time - start_time
## Time difference of 0.5061 secs</code></pre>
<p>Notice it creates a neat little message for the time difference.</p>
</div>
<div>
<h3>2) The tictoc package</h3>
<p>You can install the <code>CRAN</code> version of <strong>tictoc</strong> via</p>
<pre><code>install.packages(&quot;tictoc&quot;)</code></pre>
<p>whilst the most recent development is available via <a rel="nofollow" href="https://github.com/collectivemedia/tictoc" target="_blank">the <strong>tictoc</strong> GitHub page</a>.</p>
<pre><code>library(&quot;tictoc&quot;)</code></pre>
<p>Like <code>Sys.time()</code>, <strong>tictoc</strong> also gives us ability to nest timings within code. However, we now have some more options to customise our timing. At it’s most basic it acts like <code>Sys.time()</code>:</p>
<pre><code>tic()
Sys.sleep(0.5)
toc()
## 0.506 sec elapsed</code></pre>
<p>Now for a more contrived example.</p>
<pre><code># start timer for the entire section, notice we can name sections of code
tic(&quot;total time&quot;) 
# start timer for first subsection
tic(&quot;Start time til half way&quot;)
Sys.sleep(2)
# end timer for the first subsection, log = TRUE tells toc to give us a message
toc(log = TRUE)
## Start time til half way: 2.037 sec elapsed</code></pre>
<p>Now to start the timer for the second subsection</p>
<pre><code>tic(&quot;Half way til end&quot;)
Sys.sleep(2)
# end timer for second subsection
toc(log = TRUE)
## Half way til end: 2.012 sec elapsed
# end timer for entire section
toc(log = TRUE)
## total time: 4.067 sec elapsed</code></pre>
<p>We can view the results as a list (<code>format = TRUE</code> returns this list in a nice format), rather than raw code</p>
<pre><code>tic.log(format = TRUE)
## [[1]]
## [1] &quot;Start time til half way: 2.037 sec elapsed&quot;
## 
## [[2]]
## [1] &quot;Half way til end: 2.012 sec elapsed&quot;
## 
## [[3]]
## [1] &quot;total time: 4.067 sec elapsed&quot;</code></pre>
<p>We can also reset the log via</p>
<pre><code>tic.clearlog()</code></pre>
<hr>
</div>
</div>
<div>
<h2>Comparing functions</h2>
<div>
<h3>1) <code>system.time()</code></h3>
<p>Why oh WHY did R choose to give <code>system.time()</code> a lower case <code>s</code> and <code>Sys.time()</code> and upper case <code>s</code>? Anyway… <code>system.time()</code> can be used to time functions without having to take note of the start and end times.</p>
<pre><code>system.time(Sys.sleep(0.5))
##    user  system elapsed 
##   0.000   0.000   0.501
system.time(Sys.sleep(1))
##    user  system elapsed 
##   0.000   0.000   1.003</code></pre>
<p>We only want to take notice of the “elapsed” time, for the definition of the “user” and “system” times see <a rel="nofollow" href="http://r.789695.n4.nabble.com/Meaning-of-proc-time-td2303263.html#a2306691" target="_blank">this thread.</a></p>
<p>For a repeated timing, we would use the <code>replicate()</code> function.</p>
<pre><code>system.time(replicate(10, Sys.sleep(0.1)))
##    user  system elapsed 
##   0.000   0.000   1.007</code></pre>
</div>
<div>
<h3>2) The microbenchmark package</h3>
<p>You can install the <code>CRAN</code> version of <strong>microbenchmark</strong> via</p>
<pre><code>install.packages(&quot;microbenchmark&quot;)</code></pre>
<p>Alternatively you can install the latest update via <a rel="nofollow" href="https://github.com/olafmersmann/microbenchmark" target="_blank">the <strong>microbenchmark</strong> GitHub page.</a></p>
<pre><code>library(&quot;microbenchmark&quot;)</code></pre>
<p>At it’s most basic, <code>microbenchmark()</code> can we used to time single pieces of code.</p>
<pre><code># times = 10: repeat the test 10 times
# unit = &quot;s&quot;: output in seconds
microbenchmark(Sys.sleep(0.1), times = 10, unit = &quot;s&quot;)
## Unit: seconds
##            expr    min     lq   mean median     uq    max neval
##  Sys.sleep(0.1) 0.1001 0.1006 0.1005 0.1006 0.1006 0.1006    10</code></pre>
<p>Notice we get a nicely formatted table of summary statistics. We can record our times in anything from seconds to nanoseconds(!!!!). Already this is better than <code>system.time()</code>. Not only that, but we can compare sections of code in an easy-to-do way and name the sections of code for an easy-to-read output.</p>
<pre><code>sleep = microbenchmark(sleepy = Sys.sleep(0.1), 
                       sleepier = Sys.sleep(0.2),
                       sleepiest = Sys.sleep(0.3),
                       times = 10, 
                       unit = &quot;s&quot;)</code></pre>
<p>As well as this (more?!) <strong>microbenchmark</strong> comes with a two built-in plotting functions.</p>
<pre><code>microbenchmark:::autoplot.microbenchmark(sleep)</code></pre>
<p><img width="400" src="http://blog.jumpingrivers.com/posts/2017/2017-11-20-timing_in_R_files/figure-html/unnamed-chunk-17-1.svg"></p>
<pre><code>microbenchmark:::boxplot.microbenchmark(sleep)</code></pre>
<p><img width="400" src="http://blog.jumpingrivers.com/posts/2017/2017-11-20-timing_in_R_files/figure-html/unnamed-chunk-17-2.svg"></p>
<p>These provide quick and efficient ways of visualising our timings.</p>
<hr>
</div>
</div>
<div>
<h2>Conclusion</h2>
<p><code>Sys.time()</code> and <code>system.time()</code> have there place, but for most cases we can do better. The <strong>tictoc</strong> and <strong>microbenchmark</strong> packages are particularly useful and make it easy to store timings for later use, and the range of options for both packages stretch far past the options for <code>Sys.time()</code> and <code>system.time()</code>. The built-in plotting functions are handy.</p>
<p>Thanks for chatting!</p>
<hr>
<p><a rel="nofollow" href="https://www.jumpingrivers.com/?utm_source=blog&utm_medium=image&utm_campaign=2017timing" target="_blank"><img src="https://i1.wp.com/blog.jumpingrivers.com/img/logos/300_125.jpg?w=456" data-recalc-dims="1"></a></p>
</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://blog.jumpingrivers.com/posts/2017/timing-in-r/"> R on The Jumping Rivers 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/xjJ72XxOB74" height="1"></a></div>