Skip to content

Instantly share code, notes, and snippets.

@jmbarbone
Created January 2, 2022 19:45
Show Gist options
  • Save jmbarbone/2327d308edcbf79bfeb10dec76cced06 to your computer and use it in GitHub Desktop.
Save jmbarbone/2327d308edcbf79bfeb10dec76cced06 to your computer and use it in GitHub Desktop.
bench marks for stringi and paste
library(stringi)
requireNamespace("ggbeeswarm") # for bench::autoplot()
#> Loading required namespace: ggbeeswarm
get_marks <- function(stringi_exp, paste_exp) {
stringi_exp <- substitute(stringi_exp)
paste_exp <- substitute(paste_exp)
suppressMessages(
bench::press(n = c(1, 10, 100, 1000), {
nums <- runif(n)
vec <- stri_rand_lipsum(n)
string <- stri_rand_strings(1, n)
bench::mark(stringi_join = eval(stringi_exp), paste = eval(paste_exp))
})
)
}
marks <- dplyr::bind_rows(
`1` = get_marks(stri_join(nums, string), paste(nums, string, sep = "")),
`2` = get_marks(stri_join(nums, string, sep=','), paste(nums, string, sep=',')),
`3` = get_marks(stri_join(nums, string, collapse='; '), paste(nums, string, sep = "", collapse='; ')),
`4` = get_marks(stri_join(nums, string, sep=',', collapse='; '), paste(nums, string, sep=',', collapse='; ')),
`5` = get_marks(stri_join(vec, string, nums, sep=','), paste(vec, string, nums, sep=',')),
`6` = get_marks(stri_join(vec, string, nums, sep=',', collapse='; '), paste(vec, string, nums, sep=',', collapse='; ')),
.id = "task"
)
print(marks, n = 48)
#> # A tibble: 48 x 15
#> task expression n min median `itr/sec` mem_alloc `gc/sec` n_itr
#> <chr> <bch:expr> <dbl> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl> <int>
#> 1 1 stringi_join 1 5.8us 6.4us 143500. 3.09KB 14.4 9999
#> 2 1 paste 1 5us 5.7us 160592. 0B 32.1 9998
#> 3 1 stringi_join 10 16.6us 17.3us 54586. 0B 5.46 9999
#> 4 1 paste 10 16.2us 17.1us 50747. 0B 5.08 9999
#> 5 1 stringi_join 100 137.9us 140.4us 6431. 1.66KB 0 3354
#> 6 1 paste 100 149.4us 151.6us 6226. 1.66KB 2.01 3091
#> 7 1 stringi_join 1000 2.52ms 2.64ms 369. 1.04MB 0 185
#> 8 1 paste 1000 3.3ms 3.44ms 278. 15.72KB 0 139
#> 9 2 stringi_join 1 6.2us 7us 133936. 0B 13.4 9999
#> 10 2 paste 1 5.1us 5.6us 168117. 0B 33.6 9998
#> 11 2 stringi_join 10 17.2us 18.1us 52147. 0B 5.22 9999
#> 12 2 paste 10 16.4us 17.1us 55660. 0B 5.57 9999
#> 13 2 stringi_join 100 140.4us 142.05us 6711. 1.66KB 0 3354
#> 14 2 paste 100 150.4us 152.2us 6221. 1.66KB 2.01 3088
#> 15 2 stringi_join 1000 2.55ms 2.63ms 369. 1.04MB 0 185
#> 16 2 paste 1000 3.3ms 3.44ms 285. 15.72KB 0 143
#> 17 3 stringi_join 1 6.1us 6.7us 138713. 0B 13.9 9999
#> 18 3 paste 1 5.2us 6us 154579. 0B 30.9 9998
#> 19 3 stringi_join 10 16.8us 17.5us 54107. 336B 5.41 9999
#> 20 3 paste 10 17.1us 17.9us 52245. 0B 5.23 9999
#> 21 3 stringi_join 100 135.8us 137.5us 6948. 12.5KB 2.01 3450
#> 22 3 paste 100 179.7us 181.7us 5225. 1.66KB 0 2612
#> 23 3 stringi_join 1000 2.51ms 2.61ms 374. 1003.02KB 0 187
#> 24 3 paste 1000 5.69ms 5.94ms 164. 1.04MB 0 82
#> 25 4 stringi_join 1 6.2us 6.9us 137145. 0B 27.4 9998
#> 26 4 paste 1 5.1us 5.7us 162612. 0B 16.3 9999
#> 27 4 stringi_join 10 17.1us 17.9us 52347. 352B 5.24 9999
#> 28 4 paste 10 17.1us 17.9us 52532. 0B 10.5 9998
#> 29 4 stringi_join 100 137.6us 139.7us 6714. 12.59KB 0 3355
#> 30 4 paste 100 180.1us 182.6us 5115. 1.66KB 2.02 2536
#> 31 4 stringi_join 1000 2.97ms 3.14ms 309. 1004.02KB 0 155
#> 32 4 paste 1000 5.74ms 6.13ms 153. 1.04MB 0 77
#> 33 5 stringi_join 1 7.2us 8.5us 99107. 520B 9.91 9999
#> 34 5 paste 1 6.2us 7.2us 123333. 0B 24.7 9998
#> 35 5 stringi_join 10 25.7us 28.4us 31466. 6.3KB 3.15 9999
#> 36 5 paste 10 29.2us 30.5us 30359. 0B 3.04 9999
#> 37 5 stringi_join 100 228.4us 231.1us 4057. 78.19KB 0 2028
#> 38 5 paste 100 288.6us 292.8us 3110. 1.66KB 0 1555
#> 39 5 stringi_join 1000 3.48ms 3.73ms 258. 1.62MB 0 129
#> 40 5 paste 1000 4.89ms 5.44ms 180. 15.72KB 0 90
#> 41 6 stringi_join 1 7.6us 8.6us 97049. 720B 9.71 9999
#> 42 6 paste 1 8.2us 9us 103935. 0B 20.8 9998
#> 43 6 stringi_join 10 26.1us 26.9us 35424. 6.18KB 3.54 9999
#> 44 6 paste 10 44.9us 48.1us 19495. 6.62KB 4.07 9584
#> 45 6 stringi_join 100 227.6us 231us 4035. 74.38KB 0 2017
#> 46 6 paste 100 468.6us 490.55us 1928. 80.1KB 0 964
#> 47 6 stringi_join 1000 3.79ms 4.4ms 223. 1.57MB 0 112
#> 48 6 paste 1000 8.86ms 10.13ms 99.1 1.62MB 0 50
#> # ... with 6 more variables: n_gc <dbl>, total_time <bch:tm>, result <list>,
#> # memory <list>, time <list>, gc <list>
ggplot2::autoplot(marks) +
ggplot2::facet_grid(ggplot2::vars(task))
#> Loading required namespace: tidyr
#' ![](vain-paca_reprex_files/figure-gfm/unnamed-chunk-2-1.png)
sessioninfo::session_info()
#> - Session info ---------------------------------------------------------------
#> setting value
#> version R version 4.1.2 (2021-11-01)
#> os Windows 10 x64 (build 19043)
#> system x86_64, mingw32
#> ui RTerm
#> language (EN)
#> collate English_United States.1252
#> ctype English_United States.1252
#> tz America/New_York
#> date 2022-01-02
#> pandoc 2.14.0.3 @ C:/Program Files/RStudio/bin/pandoc/ (via rmarkdown)
#>
#> - Packages -------------------------------------------------------------------
#> package * version date (UTC) lib source
#> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0)
#> backports 1.4.1 2021-12-13 [1] CRAN (R 4.1.2)
#> beeswarm 0.4.0 2021-06-01 [1] CRAN (R 4.1.1)
#> bench 1.1.2 2021-11-30 [1] CRAN (R 4.1.2)
#> cli 3.1.0 2021-10-27 [1] CRAN (R 4.1.1)
#> colorspace 2.0-2 2021-06-24 [1] CRAN (R 4.1.0)
#> crayon 1.4.2 2021-10-29 [1] CRAN (R 4.1.1)
#> DBI 1.1.2 2021-12-20 [1] CRAN (R 4.1.0)
#> digest 0.6.29 2021-12-01 [1] CRAN (R 4.1.2)
#> dplyr 1.0.7 2021-06-18 [1] CRAN (R 4.1.0)
#> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0)
#> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0)
#> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.0)
#> farver 2.1.0 2021-02-28 [1] CRAN (R 4.1.0)
#> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.1.0)
#> fs 1.5.2 2021-12-08 [1] CRAN (R 4.1.2)
#> generics 0.1.1 2021-10-25 [1] CRAN (R 4.1.1)
#> ggbeeswarm 0.6.0 2017-08-07 [1] CRAN (R 4.1.2)
#> ggplot2 3.3.5 2021-06-25 [1] CRAN (R 4.1.0)
#> glue 1.6.0 2021-12-17 [1] CRAN (R 4.1.2)
#> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.0)
#> highr 0.9 2021-04-16 [1] CRAN (R 4.1.0)
#> htmltools 0.5.2 2021-08-25 [1] CRAN (R 4.1.1)
#> knitr 1.37 2021-12-16 [1] CRAN (R 4.1.2)
#> lifecycle 1.0.1 2021-09-24 [1] CRAN (R 4.1.1)
#> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0)
#> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0)
#> pillar 1.6.4 2021-10-18 [1] CRAN (R 4.1.1)
#> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0)
#> profmem 0.6.0 2020-12-13 [1] CRAN (R 4.1.0)
#> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.0)
#> R.cache 0.15.0 2021-04-30 [1] CRAN (R 4.1.0)
#> R.methodsS3 1.8.1 2020-08-26 [1] CRAN (R 4.1.0)
#> R.oo 1.24.0 2020-08-26 [1] CRAN (R 4.1.0)
#> R.utils 2.11.0 2021-09-26 [1] CRAN (R 4.1.1)
#> R6 2.5.1 2021-08-19 [1] CRAN (R 4.1.0)
#> reprex 2.0.1 2021-08-05 [1] CRAN (R 4.1.0)
#> rlang 0.4.12 2021-10-18 [1] CRAN (R 4.1.2)
#> rmarkdown 2.11 2021-09-14 [1] CRAN (R 4.1.1)
#> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0)
#> scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.0)
#> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.1.2)
#> stringi * 1.7.6 2021-11-29 [1] CRAN (R 4.1.2)
#> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.0)
#> styler 1.6.2 2021-09-23 [1] CRAN (R 4.1.1)
#> tibble 3.1.6 2021-11-07 [1] CRAN (R 4.1.2)
#> tidyr 1.1.4 2021-09-27 [1] CRAN (R 4.1.1)
#> tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.0)
#> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.1.0)
#> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0)
#> vipor 0.4.5 2017-03-22 [1] CRAN (R 4.1.2)
#> withr 2.4.3 2021-11-30 [1] CRAN (R 4.1.2)
#> xfun 0.29 2021-12-14 [1] CRAN (R 4.1.2)
#> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0)
#>
#> [1] C:/Users/jmbar/Documents/R/win-library/4.1
#> [2] C:/Program Files/R/R-4.1.2/library
#>
#> ------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment