Function 在基准测试期间多次在R中运行函数
我试图使用Function 在基准测试期间多次在R中运行函数,function,r,benchmarking,Function,R,Benchmarking,我试图使用system.time()测量R中函数的计算时间。 我想运行该函数几百次以获得平均值,但我不想 复制和粘贴那么多次。有更简单的方法吗?您希望使用该软件包及其功能benchmark(),它可以为您做任何事情 以下是其帮助页面中的第一个示例: R> example(benchmark) bnchmrR> # example 1 bnchmrR> # benchmark the allocation of one 10^6-element numeric vector,
system.time()
测量R中函数的计算时间。
我想运行该函数几百次以获得平均值,但我不想
复制和粘贴那么多次。有更简单的方法吗?您希望使用该软件包及其功能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
对于真正的表达式级基准测试,还有一个包
或者:(嘿,我不羞于有和德克一样的答案。)
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)
我喜欢用
require(lattice)在格子中绘制它;bwplot(expr~time,m,auto.key=TRUE,xlim=quantile(v$time,c(0,0.95))
或require(latticeExtra);ecdfplot(~time,groups=expr,m,auto.key=TRUE,xlim=quantile(v$time,c(0,0.95))
@Marek我同意默认的绘图看起来很糟糕。。。。前几天,当我在一个实际的例子中使用它时,我使用了ggplot
并做了四分位数:注意上面使用的autoplot.microbenchmark
现在可以在taRifx
1.0.3中使用,它应该在接下来的几个小时内在CRAN上可用。
require(rbenchmark)
benchmark( stuff... ) # Nice for comparative work
> 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)
autoplot(m)