Skip to content

Instantly share code, notes, and snippets.

@brodieG
Created July 2, 2017 14:55
Show Gist options
  • Save brodieG/01bdb9b664c39cd892e9cd755be0e449 to your computer and use it in GitHub Desktop.
Save brodieG/01bdb9b664c39cd892e9cd755be0e449 to your computer and use it in GitHub Desktop.
`valaddin` vs `vetr` performance.

Snippet used to compare valaddin to vetr performance. This is not actually generated as part of vignettes because it will then require valaddin as part of the build package, which brings in unwanted dependencies (via suggests) that make the build process slow on travis.

secant <- function(f, x, dx) (f(x + dx) - f(x)) / dx

secant_valaddin <- valaddin::firmly(secant, list(~x, ~dx) ~ is.numeric)

secant_stopifnot <- function(f, x, dx) {
  stopifnot(is.numeric(x), is.numeric(dx))
  secant(f, x, dx)
}

secant_vetr <- function(f, x, dx) {
  vetr(x=numeric(), dx=numeric())
  secant(f, x, dx)
}

library(microbenchmark)
microbenchmark(
  secant_valaddin(log, 1, .1),
  secant_stopifnot(log, 1, .1),
  secant_vetr(log, 1, .1)
)

Output:

Unit: microseconds
                          expr     min       lq      mean   median       uq
  secant_valaddin(log, 1, 0.1) 180.375 185.9195 361.72637 188.7040 195.0770
 secant_stopifnot(log, 1, 0.1)   8.761  10.8530  69.16612  11.9135  12.7465
      secant_vetr(log, 1, 0.1)  17.957  19.7965  49.38454  22.5960  24.7760
       max neval
 14442.845   100
  3068.107   100
  2444.288   100

Info:

> packageVersion('valaddin')
[1] ‘0.1.0’
> packageVersion('vetr')
[1] ‘0.0.2.9013’
> sessionInfo()
R version 3.4.0 (2017-04-21)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.5

Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] vetr_0.0.2.9013        unitizer_1.4.3         microbenchmark_1.4-2.1

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.11     compiler_3.4.0   plyr_1.8.4       tools_3.4.0     
 [5] digest_0.6.12    memoise_1.1.0    evaluate_0.10    tibble_1.3.3    
 [9] gtable_0.2.0     rlang_0.1.1      DBI_0.6-1        diffobj_0.1.6   
[13] commonmark_1.2   yaml_2.1.14      withr_1.0.2      stringr_1.2.0   
[17] dplyr_0.5.0      roxygen2_6.0.1   xml2_1.1.1       knitr_1.16      
[21] desc_1.1.0       devtools_1.13.2  rprojroot_1.2    grid_3.4.0      
[25] R6_2.2.1         rmarkdown_1.5    valaddin_0.1.0   ggplot2_2.2.1   
[29] purrr_0.2.2.2    magrittr_1.5     scales_0.4.1     backports_1.1.0 
[33] htmltools_0.3.6  assertthat_0.2.0 colorspace_1.3-2 stringi_1.1.5   
[37] lazyeval_0.2.0   munsell_0.4.3    crayon_1.3.2    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment