【发布时间】:2011-09-09 20:54:03
【问题描述】:
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
【问题讨论】:
-
我想你已经想到了
proc.time,因为system.time是你需要的。 -
对于更大的功能,
Rprof很好。它提供了代码块/函数中所有进程的概要文件。 -
新 R 用户通过 google 找到这个问题:
require(microbenchmark)现在(从几年前开始)是社区标准的计时方式。times <- microbenchmark( lm(y~x), glm(y~x), times=1e3); example(microbenchmark)。这会对lm与glm进行超过1000 次尝试的统计比较,而不是system.time仅测试一次。 -
使用
res <- microbenchmark(your code1,your code2)然后print(res)查看表格或ggplot2::autoplot(res)查看箱线图! ref