9

I'm looking for a way to store running times in a variable in R. In MATLAB one can do something along the lines:

tic;
...
x=toc;

and then the running time is stored in the variable x. I have tried doing the same thing with the tic() toc() function in R coming from the MATLAB-package without success. Furthermore I can't see how this can be done using the system.time() function of R neither. Any help here is much appreciated.

Stefan Hansen
  • 499
  • 1
  • 3
  • 16

4 Answers4

15

More similar to tic and toc and sometimes handier e.g. for status messages in loops:

start <- Sys.time ()
do.something ()
Sys.time () - start
cbeleites unhappy with SX
  • 13,717
  • 5
  • 45
  • 57
14

Use the built-in system.time function:

tm1 <- system.time(
{
  #your code here
})

or, alternatively the benchmark function from rbenchmark package:

tm2 <- benchmark(
{
  #your code here
}, replications=1)
digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • Oh, it was that simple. Thanks :) – Stefan Hansen Jun 05 '12 at 08:10
  • I prefer using `system.time` function because: 1. It encapsulates the code that I want to time; 2. In-built function, loading libraries like `tictoc`, `rbenchmark` unnecessary. – Sumax Oct 15 '19 at 07:45
5

Or you can do as it is described in the 'tictoc' package.

tic("timer")
1+1
toc(log = TRUE, quiet = TRUE)
log.txt <- tic.log(format = TRUE)
tic.clearlog()

your output is then stored in log.txt. You can unlist(log.txt) and analyse it as a string if you only want the time in seconds.

Cheers,

micstr
  • 5,080
  • 8
  • 48
  • 76
AleRuete
  • 313
  • 3
  • 7
4

The tictoc package implements this exact functionality, so the timing of sequential or nested timings is stored in a list and for subsequent analysis.

For example, to time each iteration of a loop and analyze the results later, use the log functionality.

library(tictoc)
tic.clearlog()
for (x in 1:10) {
    # passing x to tic() makes it a label at time of the matching toc() call.
    tic(x)
    Sys.sleep(1)
    # When log = TRUE, toc() pushes the measured timing to a list
    # quiet = TRUE prevents from printing the timing
    toc(log = TRUE, quiet = TRUE)
}

Fetch the results of toc() as formatted text for printing.

log.txt <- tic.log(format = TRUE)

Extract the list containing measurements in raw format.

log.lst <- tic.log(format = FALSE)

Since the data is already extracted, clear the tictoc log.

tic.clearlog()

Convert the list elements to timings. Each element of the list has a start (tic) and end (toc) timestamp.

timings <- unlist(lapply(log.lst, function(x) x$toc - x$tic))

Compute the average loop time.

mean(timings)
# [1] 1.001

Print the text output - note that the prefixes are the values of x.

writeLines(unlist(log.txt))
# 1: 1.002 sec elapsed
# 2: 1 sec elapsed
# 3: 1.002 sec elapsed
# 4: 1.001 sec elapsed
# 5: 1.001 sec elapsed
# 6: 1.001 sec elapsed
# 7: 1.001 sec elapsed
# 8: 1.001 sec elapsed
# 9: 1.001 sec elapsed
# 10: 1 sec elapsed
  • An imho more elegant conversion of the results into a dataframe could be done using `purrr` with `log.st %>% map_df(~as_data_frame(.x)) %>% mutate(elapsed=toc-tic)` – Holger Brandl Apr 03 '20 at 06:01
  • Thanks, @HolgerBrandl. A minor correction in the code: `library(tidyverse); log.lst %>% map_df(~as.data.frame(.x)) %>% mutate(elapsed=toc-tic)` – Sergei Izrailev Apr 06 '20 at 02:54