Function processing time

5

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

I read about the topic in the R help and at: link

But it does not 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 make graphs of performances as well?

An example:

exemplo = function(x){
  res= 0
  for(i in 1 : length(x)){
    res = res + x[i]
  }
  print(res)
  res_raiz = sqrt(abs(res))
  return(res/res_raiz)
}
teste = rnorm(10000)
exemplo(teste)
    
asked by anonymous 29.04.2016 / 11:10

2 answers

5

Your test sample is very fast, so profiling will have a lot of difficulty. Let's generate a larger test vector to make the test take longer:

teste = rnorm(10000000)

The basic profiling of R can be done with the Rprof() function.

Rprof()
exemplo(teste)
Rprof(NULL)
summaryRprof()
$by.self
          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

$by.total
          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

$sample.interval
[1] 0.02

$sampling.time
[1] 4.64

With this test size, Rprof() already shows + , which is where you spend a lot of 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:

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

And then the same information of Rprof() appears only in a visual way. The new version of RStudio will come with this integrated.

    
29.04.2016 / 19:00
3

A good option for you is the lineprof package from Hadley Wickham. Since the package is not in CRAN, you have to install from GitHub using the package devtools :

install.packages("devtools")
library(devtools)
install_github("hadley/lineprof")
library(lineprof)

This installation may take a while, especially if you need to download RTools (which is a set of programs needed to install a source package installed outside of R).

lineprof makes profiling line by line of code. To use it, your function must be in a separate .R file, and be loaded using source() :

Example.R file (I have modified your example to have a more useful output, the function does not work well with for and it will not work for very fast operations):

exemplo = function(x){
  res= 0
  sapply(seq_along(x), function(i) {
    res <- i + res
    })
  res_raiz = replicate(1000, sqrt(abs(res)))
  return(res/res_raiz)
}

Finally, to do profiling, you can go to another file or the console:

source("exemplo.R")
lp <- lineprof(exemplo(rnorm(1e5)))
lp
# time  alloc release dups                           ref
# 1 20.599 64.278  35.223    2         c("sapply", "lapply")
# 2  0.002  0.001   0.000    0                      "sapply"
# 3  1.050  0.320   0.000    1 c("sapply", "simplify2array")
# 4  0.001  0.002   0.000    0                  character(0)
# 5  0.192  0.656   0.000    2      c("replicate", "sapply")
# 6  0.001  0.001   0.000    0                           "/"
# 7  0.012  0.001   0.000    0                  character(0)

In addition to this simple view, you can also browse results using the shiny :

shine(lp)

In this view, you can navigate the levels of functions to see what is slowest internally.

You can see more details about proofing and use of this function in Hadley website >.

    
29.04.2016 / 21:30