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?
This one is good:
Taken from here.
Place
start_time
before your code andend_time
after your code.i.e.
1) Timing is operating-system dependent. On Windows you may only get milliseconds.
2) No need to define
tic()
andtoc()
, R hassystem.time()
. Here is an example: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:
3.2) microbenchmark excels at highest precision measurements:
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.