在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
。 - MarekRprof
是一个不错的选择。它可以提供代码块/函数中所有进程的概要信息。 - Rich Scrivenrequire(microbenchmark)
现在(自几年前起)是衡量效率的社区标准方法。times <- microbenchmark( lm(y~x), glm(y~x), times=1e3); example(microbenchmark)
这将对lm
和glm
进行 1000 次统计比较,而不是仅进行一次system.time
测试。 - isomorphismesres <- microbenchmark(你的代码1, 你的代码2)
,然后使用print(res)
查看表格或者使用ggplot2::autoplot(res)
查看箱线图!参考链接 - Travis