<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/profiling-with-rstudio-and-profvis/"></div></div><div><a data-url="http://www.r-bloggers.com/profiling-with-rstudio-and-profvis/" data-counturl="http://www.r-bloggers.com/profiling-with-rstudio-and-profvis/" data-count="horizontal" data-text="Profiling with RStudio and profvis" 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://blog.rstudio.org/2016/05/23/profiling-with-rstudio-and-profvis/"> RStudio Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>“How can I make my code faster?” If you write R code, then you’ve probably asked yourself this question. A profiler is an important tool for doing this: it records how the computer spends its time, and once you know that, you can focus on the slow parts to make them faster.</p>
<p>The <a target="_blank" href="https://www.rstudio.com/products/rstudio/download/preview/">preview releases</a> of RStudio now have integrated support for profiling R code and for visualizing profiling data. R itself has long had a built-in profiler, and now it’s easier than ever to use the profiler and interpret the results.</p>
<p>To profile code with RStudio, select it in the editor, and then click on <strong>Profile -> Profile Selected Line(s)</strong>. R will run that code with the profiler turned on, and then open up an interactive visualization.</p>
<p><a target="_blank" href="https://rstudioblog.files.wordpress.com/2016/05/profile1.gif"><img height="422" width="490" class="wp-image-2833" src="https://rstudioblog.files.wordpress.com/2016/05/profile1.gif?w=490&h=422"></a></p>
<p>In the visualization, there are two main parts: on top, there is the code with information about the amount of time spent executing each line, and on the bottom there is a <em>flame graph</em>, which shows R was doing over time. In the flame graph, the horizontal direction represents time, moving from left to right, and the vertical direction represents the <em>call stack</em>, which are the functions that are currently being called. (Each time a function calls another function, it goes on top of the stack, and when a function exits, it is removed from the stack.)</p>
<p><img height="388" alt="profile.png" width="592" class="wp-image-2825" src="https://rstudioblog.files.wordpress.com/2016/05/profile.png?w=592&h=388"></p>
<p>The <strong>Data</strong> tab contains a call tree, showing which function calls are most expensive:</p>
<p><a target="_blank" href="https://rstudioblog.files.wordpress.com/2016/05/data1.png"><img height="270" alt="Profiling data pane" width="490" class="wp-image-2835" src="https://rstudioblog.files.wordpress.com/2016/05/data1.png?w=490&h=270"></a></p>
<p>Armed with this information, you’ll know what parts of your code to focus on to speed things up!</p>
<p>The interactive profile visualizations are created with the <a target="_blank" href="https://rstudio.github.io/profvis/">profvis</a> package, which can be used separately from the RStudio IDE. If you use profvis outside of RStudio, the visualizations will open in a web browser.</p>
<p>To learn more about interpreting profiling data, check out the <a target="_blank" href="https://rstudio.github.io/profvis/">profvis website</a>, which has interactive demos. You can also find out more about <a target="_blank" href="http://rstudio.github.io/profvis/rstudio.html">profiling with RStudio</a> there.</p>
<p> <a target="_blank" rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/rstudioblog.wordpress.com/2819/"><img alt="" src="http://feeds.wordpress.com/1.0/comments/rstudioblog.wordpress.com/2819/" border="0"></a> <img height="1" alt="" width="1" src="https://pixel.wp.com/b.gif?host=blog.rstudio.org&blog=18843819&%23038;post=2819&%23038;subd=rstudioblog&%23038;ref=&%23038;feed=1" border="0"></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="http://www.r-bloggers.com/profiling-with-rstudio-and-profvis/"></div></div><div><a data-url="http://www.r-bloggers.com/profiling-with-rstudio-and-profvis/" data-counturl="http://www.r-bloggers.com/profiling-with-rstudio-and-profvis/" data-count="vertical" data-text="Profiling with RStudio and profvis" 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://blog.rstudio.org/2016/05/23/profiling-with-rstudio-and-profvis/"> RStudio Blog</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/FMxuz4ON0iY"></a></div>