在R中获得以毫秒为单位的执行时间

时间:2022-05-08 03:49:50

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

我已经使用tic(),toc()函数阅读了这个解决方案

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?

如何获得更多小数或更精确?

3 个解决方案

#1


30  

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

1)时序取决于操作系统。在Windows上,您可能只需几毫秒。

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

2)无需定义tic()和toc(),R具有system.time()。这是一个例子:

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)有优秀的附加软件包rbenchmark和microbenchmark。

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

3.1)rbenchmark对命令的比较特别有用,但也可以直接使用:

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:

3.2)microbenchmark在最高精度测量方面表现优异:

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.

最后一个,特别是在Linux上,已经给你纳秒。它还可以绘制结果等,因此请仔细查看该包。

#2


5  

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.

从这里开始。

#3


0  

Place start_time before your code and end_time after your code.

将start_time放在代码之前,将end_time放在代码之后。

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)

#1


30  

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

1)时序取决于操作系统。在Windows上,您可能只需几毫秒。

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

2)无需定义tic()和toc(),R具有system.time()。这是一个例子:

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)有优秀的附加软件包rbenchmark和microbenchmark。

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

3.1)rbenchmark对命令的比较特别有用,但也可以直接使用:

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:

3.2)microbenchmark在最高精度测量方面表现优异:

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.

最后一个,特别是在Linux上,已经给你纳秒。它还可以绘制结果等,因此请仔细查看该包。

#2


5  

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.

从这里开始。

#3


0  

Place start_time before your code and end_time after your code.

将start_time放在代码之前,将end_time放在代码之后。

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)