Function processing time

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 help of R and in: http://www.stat.berkeley.edu / ~nolan/stat133/Fall05/lectures/profilingEx.html

But it doesn't show the subfunctions of my function, however it shows a lot of functions that I'm not even using (not that I know of). Is it possible to make graphs of the 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)
 5
Author: Wagner Jorge, 2016-04-29

2 answers

Your test example is very fast, so profiling will have a hard time driving 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 function Rprof().

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 it +, which is where you spend a good part of your time (because of the loop).

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

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

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

 5
Author: Carlos Cinelli, 2016-04-30 13:23:52

A good option for you is the package lineprof of Hadley Wickham. Since the package is not in CRAN, you have to install it 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 code package, installed outside of R).

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

Example File.R (I modified your example to have a more useful output, the function does not work well with for and also will not have Result 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 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 the results using the package shiny:

shine(lp)

Nessa preview you can navigate through the function levels to internally see what is slower.

You can see more details about proofing and the use of this function on the Hadley website.

 3
Author: Molx, 2016-04-29 19:30:34