Eng*_*ica 8 memory algorithm r microbenchmark
背景:
这是 R 的“微基准测试”包:https :
//cran.r-project.org/web/packages/microbenchmark/index.html
参考手册中的第一行说它是为“精确计时功能”而构建的。
与此有关的一个问题是固有的计算机时间与计算机内存的权衡。一些解决方案是内存密集型的,但 CPU 速度很快。有些是 CPU 密集型的,但内存占用非常小。
问题:
我如何以良好的分辨率同时对基准/微基准进行基准测试/微基准测试,不仅是执行时间,还包括在 R 中执行期间的内存使用?
迟到总比不到好:您可以bench::mark()用来测量代码(以及更多变量)的时间和内存使用情况。
即,(取自 的帮助页面?mark)
library(bench)
dat <- data.frame(x = runif(100, 1, 1000), y=runif(10, 1, 1000))
mark(
dat[dat$x > 500, ],
dat[which(dat$x > 500), ],
subset(dat, x > 500)
)
#> # A tibble: 3 x 6
#> expression min median `itr/sec` mem_alloc `gc/sec`
#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>
#> 1 dat[dat$x > 500, ] 21.7µs 23.6µs 40663. 4.15KB 89.7
#> 2 dat[which(dat$x > 500), ] 22.2µs 24.1µs 40228. 2.77KB 92.7
#> 3 subset(dat, x > 500) 36µs 39.2µs 23867. 20.12KB 86.2
Run Code Online (Sandbox Code Playgroud)
由reprex 包(v0.3.0)于 2020 年 3 月 2 日创建