Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing tic toc values in R

Tags:

r

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.

like image 253
Stefan Hansen Avatar asked Jun 05 '12 07:06

Stefan Hansen


3 Answers

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

start <- Sys.time ()
do.something ()
Sys.time () - start
like image 174
cbeleites unhappy with SX Avatar answered Oct 12 '22 10:10

cbeleites unhappy with SX


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)
like image 39
digEmAll Avatar answered Oct 12 '22 10:10

digEmAll


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,

like image 43
AleRuete Avatar answered Oct 12 '22 10:10

AleRuete