get execution time in milliseconds in R
Asked Answered
E

4

23

I have read a solution to this using tic(), toc() functions

tic <- function(gcFirst = TRUE, type=c("elapsed", "user.self", "sys.self"))
{
   type <- match.arg(type)
   assign(".type", type, envir=baseenv())
   if(gcFirst) gc(FALSE)
   tic <- proc.time()[type]         
   assign(".tic", tic, envir=baseenv())
   invisible(tic)
}

toc <- function()
{
   type <- get(".type", envir=baseenv())
   toc <- proc.time()[type]
   tic <- get(".tic", envir=baseenv())
   print(toc - tic)
   invisible(toc)
}




tic();
-----code----
toc();


elapsed 
   0.15 

But I would like to get a lot of precision in milliseconds?

Also I was using this

ptm <- proc.time()
---code
proc.time() - ptm

and get this

   user  system elapsed 
   1.55    0.25    1.84 

How to get more decimals or more precision?

Evelyne answered 25/9, 2011 at 16:42 Comment(1)
From the help page for proc.time: "The resolution of the times will be system-specific and on Unix-alikes times are rounded to the nearest 1ms."Andreaandreana
U
37

1) Timing is operating-system dependent. On Windows you may only get milliseconds.

2) No need to define tic() and toc(), R has system.time(). Here is an example:

R> system.time(replicate(100, sqrt(seq(1.0, 1.0e6))))
   user  system elapsed 
  2.210   0.650   2.867 
R> 

3) There are excellent add-on packages rbenchmark and microbenchmark.

3.1) rbenchmark is particularly useful for comparison of commands, but can also be used directly:

R> library(rbenchmark)
R> x <- seq(1.0, 1.0e6); benchmark(sqrt(x), log(x))
     test replications elapsed relative user.self sys.self user.child sys.child
2  log(x)          100   5.408  2.85835      5.21     0.19          0         0
1 sqrt(x)          100   1.892  1.00000      1.62     0.26          0         0
R>

3.2) microbenchmark excels at highest precision measurements:

R> library(microbenchmark)
R> x <- seq(1.0, 1.0e6); microbenchmark(sqrt(x), log(x))
Unit: nanoseconds
     expr      min       lq   median       uq      max
1  log(x) 50589289 50703132 55283301 55353594 55917216
2 sqrt(x) 15309426 15412135 15452990 20011418 39551819
R> 

and this last one, particularly on Linux, already gives you nano-seconds. It can also plot results etc so have a closer look at that package.

Urion answered 25/9, 2011 at 16:46 Comment(3)
If I have more than one line of code that I'd like to measure how would I use system.time(replicate(1000, ----several lines of code------ )) ?Evelyne
Use curly brackets : system.time(replicate(1000, { ----several lines of code------ )})Twombly
Or system.time({ ----several lines of code------ })Twombly
H
7

This one is good:

options(digits.secs = 6) # This is set so that milliseconds are displayed

start.time <- Sys.time()

...Relevant code...

end.time <- Sys.time()
time.taken <- end.time - start.time
time.taken

Taken from here.

Hatter answered 15/4, 2016 at 11:50 Comment(3)
This will not provide the execution time accurate to milliseconds. The answer is just a copy of another except that answer is more appropriate for the question.Ataractic
Hmmm... Strange :) I just timed my own code and the result was: "Time difference of 1.47222 secs" Now I am curious what milliseconds are in your opinion?Hatter
You need to set options(digits.secs=6) to see Sys.time() does contain millisecond data. If not, it will only show the second level data, which confuse people into thinking this does not have millisecond info.Cameroncameroon
T
2

Take the difference of two Sys.time()s with a units= argument, ie.

start_time <- Sys.time()
## ... code here ...
end_time <- Sys.time()
as.numeric(difftime(end_time, start_time, units="secs")) * 1000

The unit of time is specified, (otherwise "secs" will be used when difftime < 1 min, or "mins" will be used when 1 min <= difftime < 1 hour). As the smallest unit available to use with difftime() is "secs", we multiply the result by 1000 thereafter.

Til answered 18/3, 2022 at 8:49 Comment(0)
L
0

Place start_time before your code and end_time after your code.

i.e.

start_time <- as.numeric(as.numeric(Sys.time())*1000, digits=15) # place at start

-----code----

end_time <- as.numeric(as.numeric(Sys.time())*1000, digits=15) # place at end

end_time - start_time    # run time (in milliseconds)
Lodie answered 23/5, 2018 at 15:54 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.