Skip to content

Instantly share code, notes, and snippets.

@tpaskhalis
Last active September 11, 2017 10:01
Show Gist options
  • Save tpaskhalis/c7af754864558ed296e25f26d7bb2185 to your computer and use it in GitHub Desktop.
Save tpaskhalis/c7af754864558ed296e25f26d7bb2185 to your computer and use it in GitHub Desktop.
R benchmarks

Comparison of two functions for creating list of arrays in R. Lambda-function implementation is about 4 times slower than using rep() function.

> larray1 <- function(nx, B) rep(list(array(data = 0.0, dim = c(nx, 1))), B)
> larray2 <- function(nx, B) lapply(1:B, function(x) x <- array(data = 0.0, dim = c(nx, 1)))
> 
> microbenchmark(
+   larray1(1211, 5),
+   larray2(1211, 5)
+ )
Unit: microseconds
             expr    min      lq     mean median     uq      max neval
 larray1(1211, 5)  4.560  5.2265 20.02225  5.441  5.787 1451.447   100
 larray2(1211, 5) 24.211 25.2730 74.86904 26.050 26.729 4847.783   100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment