【问题标题】:R equivalent of microbenchmark that includes memory as well as runtimeR 相当于微基准,包括内存和运行时
【发布时间】:2018-08-01 17:31:43
【问题描述】:

背景:
这是 R 的“微基准”包: https://cran.r-project.org/web/packages/microbenchmark/index.html

reference manual 的第一行说它是为“精确计时功能”而构建的。

其中一个问题是内在的computer-time vs. computer-memory trade-off。一些解决方案是内存密集型的,但 CPU 速度很快。有些是 CPU 密集型的,但内存占用非常小。

问题:
我如何以良好的分辨率同时对基准/微基准进行基准测试,不仅是执行时间,而且是在 R 中执行期间的内存使用?

【问题讨论】:

  • 我喜欢这个问题,但我觉得它离题了...
  • 是:“请我们推荐或查找书籍、工具、软件库”stackoverflow.com/help/on-topic
  • profvis 包会进行一些内存分析,但我认为比较多个解决方案的内存使用情况并不容易。
  • 我很高兴看到这一点,这就是为什么我说我喜欢这个问题(希望它可能是规则的例外)。感谢您的坚持,我希望看到一个好的答案(因为我没有一个副手)。 (关于澄清开/关主题的重要一点,不确定我是否还记得那部分。)
  • EngrStudent,如果您找到一种方法来进行更简单的内存使用顶级比较,类似于microbenchmark,我建议(请求?)您提供一个快速的自我回答。如果你没有时间,也许你可以推荐一个 profvis issue 来推荐这个用例,我知道我会“+1”它。

标签: r algorithm memory microbenchmark


【解决方案1】:

迟到总比不到好:您可以使用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 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-20
    • 2010-12-18
    • 1970-01-01
    • 2017-07-07
    • 1970-01-01
    • 2016-09-23
    相关资源
    最近更新 更多