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

以毫秒为单位获取R中的执行时间,r,benchmarking,execution-time,milliseconds,R,Benchmarking,Execution Time,Milliseconds,我已经阅读了使用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] as

我已经阅读了使用
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 
如何获得更多的小数或更高的精度

1)计时取决于操作系统。在Windows上,您可能只能获得毫秒

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

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

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>
R>库(rbenchmark)
R> x
3.2)在最高精度测量方面表现出色:

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> 
R>库(微基准)
R> x
最后一个,特别是在Linux上,已经给了你纳秒。它还可以绘制结果等,以便更仔细地查看该包。

这一个很好:

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
options(digits.secs=6)#设置此选项以显示毫秒

start.time将
开始时间
放在代码之前,将
结束时间
放在代码之后

i、 e


start\u time如果我有多行代码要测量如何使用
system.time(复制(1000,---多行代码)--
?从
proc.time
的帮助页面中:“时间的分辨率将是系统特定的,在Unix alikes上,时间四舍五入到最接近的1ms。”这不会提供精确到毫秒的执行时间。答案只是另一个答案的副本,只是这个答案更适合这个问题。嗯。。。奇怪:)我刚刚对自己的代码进行了计时,结果是:“时差1.47222秒”。现在我很好奇你认为毫秒是多少?你需要设置选项(digits.secs=6)来查看Sys.Time()是否包含毫秒数据。如果没有,它将只显示二级数据,这会让人们误以为这没有毫秒信息。
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> 
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
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)