Processing time of functions

Asked

Viewed 1,742 times

5

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

I read about it in R help 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 many functions that I’m not even using (not that I know of). It is possible to make performance charts also?

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)
  • 1

    Can you put an example of a function you want to evaluate? The most "crude" method is to simply save the Sys.time() before and after, but as you have seen there are more robust solutions.

  • Wagner, I appreciate you flagging my answer as correct, but I suggest you switch to Carlos'. The package I mentioned is no longer under development and this response may become invalid at any time, the answer.

  • 1

    Jeez, I made a mistake. I thank you for the numerous help and sincerity. You are saving my master’s degree, hehehehe.

2 answers

5


Your test example is very fast, so Ofiling will have a hard time driving a lot of things. Let’s generate a larger test vector to make the test take longer:

teste = rnorm(10000000)

Basic Profiling of R 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 size of test the Rprof() already shows the +, which is where you spend most of the time (because of the loop).

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

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

And then comes the same information from Rprof() only in a visual way. The new version of Rstudio will already come with this integrated.

  • Cinelli, R says this package, profvis, there is no. package ‘profvis’ is not available (for R version 3.2.0)

  • @Wagnerjorge the profvis is still only on github, to install: devtools::install_github("rstudio/profvis")

  • I set up the devtools and used the command, but gave the following error: * installing *source* package 'profvis' ...&#xA;** R&#xA;** inst&#xA;** preparing package for lazy loading&#xA;Error in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]) : &#xA; there is no package called 'digest'&#xA;ERROR: lazy loading failed for package 'profvis'&#xA;* removing 'C:/Users/Wagner/Documents/R/win-library/3.2/profvis'

  • 1

    @Wagnerjorge you have to install the package digest also manually, because it is not listed as dependency. Just run install.packages("digest").

3

A good option for you is the package lineprof 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).

The lineprof makes the Profiling line by line of code. To use it, its function must be in a separate. R file, 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 make the Profiling, you can go in 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 preview, you can also browse the results using the package shiny:

shine(lp)

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

You can see more details about proofing and the use of this function in hadley’s website.

  • 1

    The lineprof has been deprecated, the package that replaced it is profvis.

  • @Carloscinelli Really, I didn’t even notice it... My answer doesn’t make much sense then, but I’ll leave since it’s already written.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.