2011-06-07 97 views
204

测量功能执行时间R中是否有标准化的方式?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 
+2

我觉得你在脑子里有'proc.time',因为'system.time'是你需要的。 – Marek 2011-06-07 08:35:24

+0

对于更大的功能,'Rprof'很好。它提供代码块/函数中所有进程的配置文件。 – 2014-08-24 17:08:24

+19

新R用户通过谷歌发现这个问题:'require(microbenchmark)'现在(自几年前)社区标准的时间方式。 times < - microbenchmark(lm(y〜x),glm(y〜x),times = 1e3);例如(微基准)'。这对'lm'和'glm'进行1000次以上的统计比较,而不是'system.time'测试一次。 – isomorphismes 2015-04-24 17:42:38

回答

145

内置功能system.time()将做到这一点。

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

+0

重要的是要知道'system.time()'有一个参数'gcFirst',默认为'TRUE'。这一方面使得测量更具有可重复性,但是可能会产生总运行时间的重大开销(这不会被测量,偏离过程)。 – 2016-10-26 15:29:32

+0

这是用什么单位测量的?例如我刚刚运行了'system.time(result < - myfunction(with,arguments))'并获得了187.564的输出 - 是几秒钟内还是什么? – zsad512 2017-09-14 00:40:01

32

上的测量的执行时间的稍微更好的方式,是使用rbenchmark包。这个软件包(很容易)允许您指定复制测试的次数,并且相对基准测试应该是。

也是一个相关的问题,请参见在stats.stackexchange

+5

Microbenchmark更好,因为它使用更高精度的定时功能。 – hadley 2011-06-07 14:45:21

+3

@hadley但rbenchmark在比较的情况下更便于用户使用。对我来说microbenchmark是升级system.time。 rmicrobenchmark是我们需要的:) – Marek 2011-06-07 15:21:53

+2

microbenchmark的维护人员相当响应 - 我敢打赌,他会添加任何你需要的。 – hadley 2011-06-07 16:03:30

9

您可以使用MATLAB风格tic - toc功能,如果你喜欢。看到这个其他SO质疑

Stopwatch function in R

+0

即将添加'proc.time()'...我更喜欢这个可爱的名字。 =) – isomorphismes 2014-09-26 06:53:24

49

正如Andrie说,system.time()工作正常。对于短期的功能,我更喜欢把replicate()它:

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

您最好使用microbenchmark软件包,因为它不包含时间复制的开销。 – hadley 2011-06-07 14:46:26

+5

http://cran.r-project.org/web/packages/microbenchmark/index.html – tim 2014-04-03 15:29:53

170

这样做的另一种可能的方式是使用Sys.time():

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

不是最优雅的方式来做到这一点,与上面的答案相比,但绝对是一种方式来做到这一点。

+9

这是更高的内存效率,然后system.time(),它有效地复制其参数。当你处理几乎不适合你的RAM的数据时,这很重要。 – 2015-12-15 15:21:33

23

还有proc.time()

您可以以同样的方式为Sys.time使用,但它给你一个类似的结果system.time

ptm <- proc.time() 
#your function here 
proc.time() - ptm 

使用

system.time({ #your function here }) 

之间的主要区别在于,proc.time()方法仍然不执行你的函数,而不是仅仅测量时间... 顺便说一句,我喜欢用system.time{}里面所以你可以把一组东西...

14

包“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 
9

虽然其他解决方案是一个单一功能非常有用,我建议下面这段代码中是比较普遍和有效的:

Rprof (tf <- "log.log", memory.profiling = TRUE) 
your code must be in between 
Rprof (NULL) ; print (summaryRprof (tf) ) 
13

microbenchmark是轻质(〜50KB)包和更多或更少的标准中的R为基准的多个表达式和函数的方式:

microbenchmark(myfunction(with,arguments)) 

例如:

> microbenchmark::microbenchmark(log10(5), log(5)/log(10), times = 10000) 
Unit: nanoseconds 
      expr min lq mean median uq max neval cld 
     log10(5) 0 0 25.5738  0 1 10265 10000 a 
log(5)/log(10) 0 0 28.1838  0 1 10265 10000 

这里两个表达式进行评价10000倍,平均执行时间为25-30纳秒。