R 等效于包括内存和运行时的微基准测试
R equivalent of microbenchmark that includes memory as well as runtime
背景:
这是 R 的 "microbenchmark" 包:
https://cran.r-project.org/web/packages/microbenchmark/index.html
reference manual 中的第一行说它是为 "Accurate Timing Functions" 构建的。
其中一个问题是内在的 computer-time vs. computer-memory trade-off。一些解决方案占用大量内存,但 CPU 速度很快。有些是 CPU 密集型,但内存占用非常小。
问题:
我如何同时以良好的分辨率 benchmark/microbenchmark 不仅是执行时间,而且是 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
由 reprex package (v0.3.0)
于 2020 年 3 月 2 日创建
背景:
这是 R 的 "microbenchmark" 包:
https://cran.r-project.org/web/packages/microbenchmark/index.html
reference manual 中的第一行说它是为 "Accurate Timing Functions" 构建的。
其中一个问题是内在的 computer-time vs. computer-memory trade-off。一些解决方案占用大量内存,但 CPU 速度很快。有些是 CPU 密集型,但内存占用非常小。
问题:
我如何同时以良好的分辨率 benchmark/microbenchmark 不仅是执行时间,而且是 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
由 reprex package (v0.3.0)
于 2020 年 3 月 2 日创建