R equivalent of microbenchmark that includes memory as well as runtime
Asked Answered
K

1

10

Background:
This is the "microbenchmark" package for R: https://cran.r-project.org/web/packages/microbenchmark/index.html

The first line in the reference manual says that it is built for "Accurate Timing Functions".

One problem with this is the intrinsic computer-time vs. computer-memory trade-off. Some solutions are memory intensive, but CPU fast. Some are CPU intensive, but have a very small memory footprint.

Question:
How do I simultaneously, and with good resolution, benchmark/microbenchmark not only the execution time, but the memory use during execution in R?

Kendy answered 22/2, 2018 at 0:22 Comment(10)
I like the question but feel it's off-topic for SO ...Romona
What is the right library for my application is off-topic? I'm looking for what should be a software tool commonly used by R-programmers.Kendy
Yes: "asking us to recommend or find a book, tool, software library", stackoverflow.com/help/on-topicRomona
The profvis package does some memory profiling, but I don't think it's as easy to compare multiple solutions on their memory usage.Restful
@Romona - The piece of text that defines whether "recommend" is on vs. off is this "...as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." Inspection will show that I gave a clear problem, and instead of inviting "opinion" or "spam", I am asking for action therefore the criterion you used does not apply.Kendy
I'm happy to see that, which is why I said I like the question (hoping it might be an exception to the rule). Thanks for persevering, I'd like to see a good answer (as I don't have one offhand). (And great point about clarifying the on/off topic-ness, not certain I had that part as well remembered.)Romona
@Restful - it looks like profvis does both time and memory! Nice.Kendy
EngrStudent, if you find a way to do simpler top-level comparison of memory use, similar to microbenchmark, I suggest (request?) you provide up a quick self-answer. If you don't have the time, perhaps you could suggest a profvis issue suggesting the use-case, I know I'd "+1" it.Romona
@Restful - It looks like the "profvis" uses this profiler: stat.ethz.ch/R-manual/R-devel/library/utils/html/Rprof.htmlKendy
@Romona - I'm very much up for that. :)Kendy
L
13

Better late than never: You can use bench::mark() to measure both time and memory usage of code (and some more variables).

I.e., (taken from the help page for ?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

Created on 2020-03-02 by the reprex package (v0.3.0)

Lomasi answered 2/3, 2020 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.