R中的测量功能执行时间

问题描述 投票:268回答:10

R中是否有标准化的方法来测量功能的执行时间?

显然,我可以在执行前后取system.time,然后取两者的差额,但是我想知道是否存在某种标准化的方式或功能(想不想发明轮子)。


我似乎记得我曾经使用过如下所示的内容:

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
r time profiling
10个回答
238
投票

执行此操作的另一种可能方法是使用Sys.time():

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

与上述回答相比,这不是最优雅的方法,但绝对是一种方法。


11
投票

另一种简单但功能强大的方法是使用软件包profvis。它不仅可以衡量代码的执行时间,还可以深入了解您执行的每个函数。它也可以用于Shiny。


176
投票

内置功能system.time()将执行此操作。

使用方式:system.time(result <- myfunction(with, arguments))


56
投票

[正如安德烈所说,system.time()正常工作。对于简短功能,我更喜欢将replicate()放入其中:

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

37
投票

衡量执行时间的一种更好的方法是使用rbenchmark程序包。该软件包(轻松地)使您可以指定复制测试次数以及相对基准应该是多少次。]

另请参阅stats.stackexchange处的相关问题


32
投票

还有proc.time()


30
投票

microbenchmark是一个轻量级(〜50kB)包,在R中或多或少是一种标准方法,用于对多个表达式和函数进行基准测试。]]

microbenchmark(myfunction(with,arguments))

24
投票

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


16
投票

尽管其他解决方案对于单个功能很有用,但我建议以下代码更通用,更有效:


11
投票

如果愿意,可以使用MATLAB样式的tic-toc函数。看到另一个SO问题

© www.soinside.com 2019 - 2024. All rights reserved.