Skip to content

bench::workout(total=) #95

@romainfrancois

Description

@romainfrancois

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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    featurea feature request or enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions