larmarange / broom.helpers

A set of functions to facilitate manipulation of tibbles produced by broom
https://larmarange.github.io/broom.helpers/
GNU General Public License v3.0
22 stars 7 forks source link

Using native pipe for speed improvement? #262

Closed larmarange closed 3 months ago

larmarange commented 3 months ago

@ddsjoberg since gtsummary now requires R 4.2, maybe it could be a good timing for using the native pipe in broom.helpers as well?

ddsjoberg commented 3 months ago

After the base R pipe was released, I recall reading an article (or tweet?) from hadley that they had re-written the magrittr pipe, and its efficiency is now similar to the base pipe. We can definitely begin to use the base pipe, but if we're going for straight efficiency gains, we could probably use {profvis} to identify places where we could get bigger gains?

library(stringr)

text <- "abcd"

fn_magrittr <- function(text) {
  text %>% str_replace("a", "b") %>%
    str_replace("b", "c") %>%
    str_replace("c", "d") 
}

fn_native <- function(text) {
  text |> str_replace("a", "b") |>
    str_replace("b", "c") |>
    str_replace("c", "d") 
}

bench::mark(
  magrittr = fn_magrittr(text),
  native = fn_native(text),
  check = FALSE
)
#> # A tibble: 2 × 6
#>   expression      min   median `itr/sec` mem_alloc `gc/sec`
#>   <bch:expr> <bch:tm> <bch:tm>     <dbl> <bch:byt>    <dbl>
#> 1 magrittr     77.9µs   82.1µs    11575.  160.26KB     27.5
#> 2 native       77.7µs   80.4µs    12147.    1.55KB     27.2

Created on 2024-07-25 with reprex v2.1.0

(Obviously, I am no expert in reducing runtime, though! 😆 )