1

When I've written stuff in Matlab, I've often greatly appreciated its "Run and Time" functionality: for those who don't know, this runs the file and upon completion outputs not only the run time, but also opens a new window showing the code, and saying how many times each line was run and how long the program spent on each line. For finding bottlenecks in my code, this has been invaluable!

I am not aware of a similar functionality in R -- whether that be an R package, or part of RStudio -- and searching using a well-known search engine has not rectified this.

Is it possible to do a similar thing for R? It would be most appreciated!

Sam OT
  • 420
  • 1
  • 4
  • 19

1 Answers1

3

It would help you if you knew that the "Run and Time" option in MATLAB is simply a user interface on top of the profile command. In particular, in MATLAB you can do

profile on
% Run some code
profile off; profile viewer % Stops profiling and opens the timing window

I say this is helpful because you can "profile" in a similar way in RStudio, via the "Profile" menu.

Please see this RStudio Support page for in depth details.

profiling


To summarise the above RStudio help page, in essence, one wants to write

profvis({
    #CODE
})

(Note that the package profvis may need to be installed.) Further details on how to use can be found by typing ?Rprof, and by visiting this related SO question: How to efficiently use Rprof in R?.

Sam OT
  • 420
  • 1
  • 4
  • 19
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • I thought this was going to be perfect, but I get error messages if I try to put `profile on` -- it says `Error: unexpected symbol in "profile on"`. Wrapping my code in `profvis({ ... })` works, though – Sam OT Sep 24 '19 at 09:41
  • @Sam The `profile on` syntax is for MATLAB, not R... I was just showing that for context. You should follow the directions in the RStudio support page for the R profiling – Wolfie Sep 24 '19 at 09:42