function in R that also returns the execution time itself

Asked

Viewed 1,583 times

4

I know there is Rprof(), but it seems to me rather inaccurate compared to microbenchmark(). However, if I want to use the microbenchmark() I have to call the function 2 times, once to have her output and another to run her time (which seems quite impractical)

I do not know how to call the function only 1 time and have as response the normal output of it and also the execution time quite accurate.

That’s not the function, but follow an example of my problem:

teste <- function(x){

  Rprof()
  x <- x+2
  Rprof(NULL)

  return(summaryRprof())
}
guarda_x_e_tempo <- teste(2)

or

teste <- function(x){
  x <- x+2
  return(x)
}
guarda_x <- teste(2)
guarda_tempo <- teste(2)
  • What is your doubt?

  • do not know how to call the function only 1 time and have as a response the normal output of it and also the execution time quite accurate

  • Edit the question and add this information. Without it, your question is incomplete and can be flagged and closed.

3 answers

2

Adenilson, all right?

I believe that the system.time function meets your need:

funcao_exemplo <- function(x) {
  print(x)
}

system.time(funcao_exemplo("Hello World"))
  • Welcome to Stackoverflow in English. It’s always nice to see new users, especially when they have things to say, but in this particular case, I had already answered this. Please continue to contribute, but first read the other answers.

  • Hey, Rui, what’s up? Contrary to what you said, I did read the other answers, however, I had the intention to meet those who seek a clear and simple answer, so I wrote a 3-line answer. I thank you for your concern, but you can rest assured that I have read and follow the rules of the forum.

2

The accuracy of microbenchmark is in running several times the functions thus avoiding being influenced by possible computer crashes that could affect the running time. When you spin the microbenchmark it runs by default 100x the function to be able to calculate average, median , echo of the running times.

That said, microbenchmark is not suitable for what you want to do.

I believe that the other answers already have good solutions, but I think an elegant way to do that would be to create a function as follows:

crono <- function(f) {
  function(...) {
    exec_time <- system.time({res <- f(...)})
    list(
      exec_time = exec_time,
      res = res
    )
  }
}

We call this kind of function Function Operators.

With it you can create versions of your functions that are timed, for example:

crono_mean <- crono(mean)
crono_mean(1:10000000)

That would result in:

$exec_time
   user  system elapsed 
  0.034   0.018   0.061 

$res
[1] 5e+06

1


See if the following is what you want.
The result of the base R function proc.time is obtained in the first function instruction teste and then subtracted from proc.time in the end.

teste <- function(x){
  ini <- proc.time()
  x <- x + 2
  list(result = x, tempo = proc.time() - ini)
}

guarda_x_e_tempo <- teste(2)

guarda_x_e_tempo
#$`result`
#[1] 4
#
#$tempo
#   user  system elapsed 
#      0       0       0

Now another function, longer.

teste2 <- function(x, n = 1e6){
  ini <- proc.time()
  for(i in seq_len(n)) x <- x + 2
  list(result = x, tempo = proc.time() - ini)
}

teste2(2)
#$`result`
#[1] 2000002
#
#$tempo
#   user  system elapsed 
#   0.08    0.00    0.08 
#

Another way is to put the expression to time on system.time. This allows you to time the execution without modifying the functions.

teste3 <- function(x, n = 1e6){
  for(i in seq_len(n)) x <- x + 2
  x
}

system.time(teste3(2))
# usuário   sistema decorrido 
#   0.048     0.000     0.049

Browser other questions tagged

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