在基准测试期间多次运行R中的函数

Dom*_*bey 14 benchmarking r function

我试图测量R中函数的计算时间system.time().我想运行该函数几百次以获得平均值,但我不想复制和粘贴那么多次.有更简单的方法吗?

Ari*_*man 17

microbenchmark软件包采用了一个,times=选项,并且具有更准确的附加功能.

> library(microbenchmark)
> m <- microbenchmark( seq(10)^2, (1:10)^2, times=10000)
> m
Unit: nanoseconds
       expr   min    lq median    uq     max
1  (1:10)^2  2567  3423   3423  4278   41918
2 seq(10)^2 44484 46195  46195 47051 1804147
> plot(m)
Run Code Online (Sandbox Code Playgroud)

绘图微基准

并使用ggplot2尚未发布的autoplot()方法:

autoplot(m)
Run Code Online (Sandbox Code Playgroud)

autoplot microbenchmark


42-*_*42- 12

system.time(replicate (  ... stuff ..) )
Run Code Online (Sandbox Code Playgroud)

或者:(嘿,我和Dirk的答案一样,我并不感到羞耻.)

require(rbenchmark)
benchmark( stuff... )   # Nice for comparative work
Run Code Online (Sandbox Code Playgroud)


Dir*_*tel 11

您想使用rbenchmark软件包及其功能benchmark(),它可以为您提供所有功能.

以下是其帮助页面中的第一个示例:

R> example(benchmark)

bnchmrR> # example 1
bnchmrR> # benchmark the allocation of one 10^6-element numeric vector, 
bnchmrR> # replicated 100 times
bnchmrR> benchmark(1:10^6)
    test replications elapsed relative user.self sys.self user.child sys.child
1 1:10^6          100   0.327        1      0.33        0          0         0
Run Code Online (Sandbox Code Playgroud)

对于真正的表达水平的标杆,也有在微基准测试包.