Skip to content

Instantly share code, notes, and snippets.

@paleolimbot
Created April 3, 2020 18:06
Show Gist options
  • Save paleolimbot/fa4ab0acdbaadcda70f0b3600efe114b to your computer and use it in GitHub Desktop.
Save paleolimbot/fa4ab0acdbaadcda70f0b3600efe114b to your computer and use it in GitHub Desktop.
library(furrr)
slow_func <- function(x) {
Sys.sleep(0.1)
x
}
bench::mark(lapply(1:10, slow_func))
# use this!
plan(multisession)
bench::mark(future_map(1:10, slow_func))
# not this!
plan(multicore)
bench::mark(future_map(1:10, slow_func))
@paleolimbot
Copy link
Author

library(furrr)
#> Loading required package: future

slow_func <- function(x) {
  Sys.sleep(0.1)
  x
}

bench::mark(lapply(1:10, slow_func))
#> # A tibble: 1 x 6
#>   expression                   min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>              <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 lapply(1:10, slow_func)    1.02s    1.02s     0.977    3.82MB        0

plan(multisession)
bench::mark(future_map(1:10, slow_func))
#> Warning: Some expressions had a GC in every iteration; so filtering is
#> disabled.
#> # A tibble: 1 x 6
#>   expression                       min  median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                  <bch:tm> <bch:t>     <dbl> <bch:byt>    <dbl>
#> 1 future_map(1:10, slow_func)    268ms   272ms      3.67    4.41MB     3.67

plan(multicore)
bench::mark(future_map(1:10, slow_func))
#> # A tibble: 1 x 6
#>   expression                       min  median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr>                  <bch:tm> <bch:t>     <dbl> <bch:byt>    <dbl>
#> 1 future_map(1:10, slow_func)    1.03s   1.03s     0.974     479KB        0

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment