Running functions in R multiple times during benchmarking

孤人 提交于 2019-12-02 19:24:51

The microbenchmark package takes a ,times= option and has the added bonus of being a bit more accurate.

> 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)

And using the not-yet-released autoplot() method for ggplot2:

autoplot(m)

system.time(replicate (  ... stuff ..) )

Or: (hey, I'm not ashamed to have the same answer as Dirk.)

require(rbenchmark)
benchmark( stuff... )   # Nice for comparative work

You want to use the rbenchmark package and its function benchmark() which does just about everything for you.

Here is the first example from its help page:

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

For truly expression-level benchmarking, there is also the microbenchmark package.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!