在R中是否有衡量函数执行时间的标准化方法?

显然我可以用系统。时间,然后取它们的差异,但我想知道是否有一些标准化的方法或功能(不想发明轮子)。


我似乎记得我曾经用过如下的东西:

somesysfunction("myfunction(with,arguments)")
> Start time : 2001-01-01 00:00:00  # output of somesysfunction
> "Result" "of" "myfunction"        # output of myfunction
> End time : 2001-01-01 00:00:10    # output of somesysfunction
> Total Execution time : 10 seconds # output of somesysfunction

当前回答

度量执行时间的一个稍微好一点的方法是使用rbenchmark包。这个包(很容易)允许您指定复制测试的次数,以及相对基准测试应该是多少次。

另见stats.stackexchange上的相关问题

其他回答

虽然其他解决方案对于单个函数也很有用,但我推荐使用下面的代码段,因为它更通用、更有效:

Rprof(tf <- "log.log", memory.profiling = TRUE)
# the code you want to profile must be in between
Rprof (NULL) ; print(summaryRprof(tf))

另一种简单但非常强大的方法是使用包profvis。它不仅测量代码的执行时间,还为您提供了执行每个函数的钻取。它也可以用于Shiny。

library(profvis)

profvis({
  #your code here
})

点击这里查看一些例子。

度量执行时间的一个稍微好一点的方法是使用rbenchmark包。这个包(很容易)允许您指定复制测试的次数,以及相对基准测试应该是多少次。

另见stats.stackexchange上的相关问题

包“tictoc”为您提供了一种非常简单的测量执行时间的方法。文档在:https://cran.fhcrc.org/web/packages/tictoc/tictoc.pdf。

install.packages("tictoc")
require(tictoc)
tic()
rnorm(1000,0,1)
toc()

要保存经过的时间到一个变量,你可以这样做:

install.packages("tictoc")
require(tictoc)
tic()
rnorm(1000,0,1)
exectime <- toc()
exectime <- exectime$toc - exectime$tic

正如Andrie所说,system.time()工作正常。对于短函数,我更喜欢在其中放入replication ():

system.time( replicate(10000, myfunction(with,arguments) ) )