Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bench::workout(total=) #95

Open
romainfrancois opened this issue Aug 13, 2020 · 0 comments
Open

bench::workout(total=) #95

romainfrancois opened this issue Aug 13, 2020 · 0 comments
Labels
feature a feature request or enhancement

Comments

@romainfrancois
Copy link
Contributor

I sometimes use workout() to disect several ways to do something, and would find an additional total row very useful.

I hacked my way to this: but perhaps there's room for an argument:

options(width = 200)
suppressPackageStartupMessages({
  library(dplyr, warn.conflicts = FALSE)
  library(purrr)
  library(vctrs)
  library(rlang)
})

workout <- function(...) {
  out <- bench::workout(...)
  bind_rows(
    mutate(out, exprs = as.character(exprs)), 
    tibble(exprs = "<total>", process = sum(out$process), real = sum(out$real))
  )
}

m0 <- matrix(0, 50, 2000)
groups <- sample(1:10, 50, replace = TRUE)

m1 <- apply(m0, c(1,2), function(x) sample(c(0,1),1)) %>%
  as.data.frame() %>%
  mutate(groups = groups)

m2 <- m1 %>%
  group_by(groups)

make_sum_expressions <- function(names) {
  map(set_names(names), ~expr(sum(!!sym(.x))))
}

eval_loop <- function(.data) {
  mask <- as_data_mask(.data)
  map(expressions, eval_tidy, data = mask)
}

workout({
  # generate expressions x = sum(x) for all, similar to what _all() does
  expressions <- make_sum_expressions(names(m2)[1:2000]) 
  indices <- group_rows(m2)
  
  # chop the entire data frame at once, this gives a list of all the cur_data_all()
  chops <- vec_chop(m2, indices)
  
  # then we "just" have to eval_tidy() the expression
  out <- map(chops, eval_loop)
  
  # turn them into a tibble
  tibbles <- map(out, as_tibble)
  
  # then bind
  bind_rows(tibbles)
})
#> # A tibble: 7 x 3
#>   exprs                                                   process     real
#>   <chr>                                                  <bch:tm> <bch:tm>
#> 1 expressions <- make_sum_expressions(names(m2)[1:2000])  15.68ms  15.72ms
#> 2 indices <- group_rows(m2)                                 217µs 218.28µs
#> 3 chops <- vec_chop(m2, indices)                          30.42ms  30.51ms
#> 4 out <- map(chops, eval_loop)                            61.21ms  61.27ms
#> 5 tibbles <- map(out, as_tibble)                          81.81ms  82.07ms
#> 6 bind_rows(tibbles)                                       9.46ms   9.48ms
#> 7 <total>                                                198.81ms 199.26ms

Created on 2020-08-13 by the reprex package (v0.3.0.9001)

@DavisVaughan DavisVaughan added the feature a feature request or enhancement label May 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants