Skip to content

Instantly share code, notes, and snippets.

@markmfredrickson
Created October 18, 2011 02:28
Show Gist options
  • Save markmfredrickson/1294468 to your computer and use it in GitHub Desktop.
Save markmfredrickson/1294468 to your computer and use it in GitHub Desktop.
Comparing the size of expand.grid with a list of functions of the same size.
gsize <- function(n) {
grid <- expand.grid(rep(list(1:10), n))
object.size(grid) / 1024
}
fsize <- function(n) {
f <- function(i) {force(i) ; function(j) { i + j}}
flst <- lapply(1:(10^n), f)
object.size(flst) / 1024
}
# a function to compare object sizes
comparison <- function(n) {
fsize(n) - gsize(n)
}
# > sapply(1:5, comparison)
# [1] 3.796875 47.585938 486.140625 4841.140625 48043.718750
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment