Recover this article http://www.win-vector.com/blog/2016/12/magrittrs-doppelganger/.
Another discussion on https://stackoverflow.com/questions/35933272/why-is-using-dplyr-pipe-slower-than-an-equivalent-non-pipe-expression/35935105
library(microbenchmark)
library(tidyverse)
## Loading tidyverse: ggplot2
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(wrapr)
set.seed(234634)
fbase <- function(d) {
d=(sqrt(tan((cos(sin(d))))))
return(d)
}
fmagrittr <- function(d) {
d<-(d %>% sin() %>% cos() %>% tan() %>% sqrt())
return(d)
}
fmagrittrdot <- function(d) {
d<-(d %>% sin(.) %>% cos(.) %>% tan(.) %>% sqrt(.))
return(d)
}
fsemicolon <- function(d) {
d ->.; sin(.) ->.; cos(.) ->.; tan(.) ->.; sqrt(.)->d;
return(d)
}
fwrapr <- function(d) {
d <-d%.>%sin(.)%.>%cos(.)%.>%tan(.)%.>%sqrt(.)
return(d)
}
input<-seq(1:30)
bm <- microbenchmark(
fbase(input),
fmagrittr(input),
fmagrittrdot(input),
fsemicolon(input),
fwrapr(input),
control=list(warmup=100L,
order='random'),
times=10000L
)
print(bm)
## Unit: microseconds
## expr min lq mean median uq max
## fbase(input) 3.072 3.414 4.714244 3.755 4.096 2803.713
## fmagrittr(input) 119.126 136.534 167.884004 143.019 169.302 4038.316
## fmagrittrdot(input) 109.910 125.611 157.384073 131.755 157.697 3674.113
## fsemicolon(input) 3.072 3.755 5.100713 4.096 4.438 3071.659
## fwrapr(input) 17.750 20.822 27.359023 22.187 24.918 3664.214
## neval cld
## 10000 a
## 10000 d
## 10000 c
## 10000 a
## 10000 b
autoplot(bm)
print(sessionInfo())
## R version 3.4.2 (2017-09-28)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 16299)
##
## Matrix products: default
##
## locale:
## [1] LC_COLLATE=English_United States.1252
## [2] LC_CTYPE=English_United States.1252
## [3] LC_MONETARY=English_United States.1252
## [4] LC_NUMERIC=C
## [5] LC_TIME=English_United States.1252
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] wrapr_1.0.0 dplyr_0.7.4 purrr_0.2.3
## [4] readr_1.1.1 tidyr_0.7.1 tibble_1.3.4
## [7] ggplot2_2.2.1 tidyverse_1.1.1 microbenchmark_1.4-2.1
## [10] RevoUtils_10.0.6 RevoUtilsMath_10.0.1
##
## loaded via a namespace (and not attached):
## [1] zoo_1.8-0 reshape2_1.4.2 splines_3.4.2 haven_1.1.0
## [5] lattice_0.20-35 colorspace_1.3-2 htmltools_0.3.6 yaml_2.1.14
## [9] survival_2.41-3 rlang_0.2.0.9000 foreign_0.8-69 glue_1.1.1
## [13] modelr_0.1.1 readxl_1.0.0 bindrcpp_0.2 multcomp_1.4-7
## [17] bindr_0.1 plyr_1.8.4 stringr_1.2.0 munsell_0.4.3
## [21] gtable_0.2.0 cellranger_1.1.0 rvest_0.3.2 mvtnorm_1.0-6
## [25] codetools_0.2-15 psych_1.7.8 evaluate_0.10.1 knitr_1.17
## [29] forcats_0.2.0 parallel_3.4.2 TH.data_1.0-8 broom_0.4.2
## [33] Rcpp_0.12.13 scales_0.5.0 backports_1.1.1 jsonlite_1.5
## [37] mnormt_1.5-5 hms_0.3 digest_0.6.12 stringi_1.1.5
## [41] grid_3.4.2 rprojroot_1.2 tools_3.4.2 sandwich_2.4-0
## [45] magrittr_1.5 lazyeval_0.2.0 pkgconfig_2.0.1 MASS_7.3-47
## [49] Matrix_1.2-11 xml2_1.1.1 lubridate_1.6.0 assertthat_0.2.0
## [53] rmarkdown_1.6 httr_1.3.1 R6_2.2.0 nlme_3.1-131
## [57] compiler_3.4.2