Skip to content

Instantly share code, notes, and snippets.

@lgatto
Created January 4, 2014 00:07
Show Gist options
  • Save lgatto/8249301 to your computer and use it in GitHub Desktop.
Save lgatto/8249301 to your computer and use it in GitHub Desktop.
## l1 is a list with 20 numeric elements of length 1e4 each
set.seed(123)
l2 <- l1 <- replicate(20,rnorm(1e4),simplify=FALSE)
length(l1)
sapply(l1,length)
## l2 is the same list with names
names(l2) <- paste0("X", 1:length(l2))
all.equal(l1, l2)
## [1] "names for current but not for target"
library("rbenchmark")
benchmark(range(l1),
range(l2),
range(sapply(l2, range)))[, 1:5]
## test replications elapsed relative user.self
## 1 range(l1) 100 0.992 1.023 0.988
## 2 range(l2) 100 31.703 32.684 31.548
## 3 range(sapply(l2, range)) 100 0.970 1.000 0.968
## Rprof indicates that it was the c primitive that accounts for the
## big different in time.
## > range.default
## function (..., na.rm = FALSE, finite = FALSE)
## {
## x <- c(..., recursive = TRUE)
## if (is.numeric(x)) {
## if (finite)
## x <- x[is.finite(x)]
## else if (na.rm)
## x <- x[!is.na(x)]
## return(c(min(x), max(x)))
## }
## c(min(x, na.rm = na.rm), max(x, na.rm = na.rm))
## }
## The line that recursively concatenates the ... arguments transforms
## the list in a numeric and names all individual elements, thus
## generating a _named_ numeric of length 20 times 10000. As
## demonstrated below, it is the creation of the names that accounts
## for the overhead.
benchmark(c(l2, recursive = TRUE),
c(l1, recursive = TRUE))[, 1:5]
## test replications elapsed relative user.self
## 2 c(l1, recursive = TRUE) 100 0.258 1.000 0.256
## 1 c(l2, recursive = TRUE) 100 27.879 108.058 27.776
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment