Function processing time


How do I check the processing time of a function's subfunctions in order to optimize it?

I read about it in the R help and at:

But it doesn't show the subfunctions of my function, however it shows many functions that I'm not even using (not that I know of). Is it possible to graph the performances as well?

An example:

exemplo = function(x){
  res= 0
  for(i in 1 : length(x)){
    res = res + x[i]
  res_raiz = sqrt(abs(res))
teste = rnorm(10000)


Your test example is very fast, so profiling will have a hard time showing a lot. Let's generate a larger test vector to make the test take longer:

teste = rnorm(10000000)

Basic R profiling can be done with the Rprof() function.

          self.time self.pct total.time total.pct
"exemplo"      4.48    96.55       4.64    100.00
"+"            0.16     3.45       0.16      3.45

          total.time total.pct self.time self.pct
"exemplo"       4.64    100.00      4.48    96.55
"+"             0.16      3.45      0.16     3.45

[1] 0.02

[1] 4.64

With this test size Rprof() already shows you the + , which is where you spend most of your time (because of the loop).

A useful package for profiling is profvis , which also uses Rprof() but makes viewing easier. To install use devtools::install_github("rstudio/profvis") . In your case you would do:

p <- profvis({exemplo(teste)})

And then the same information from Rprof() , only in a visual way. The new version of RStudio will already have this integrated.

Scroll to Top