Skip to content

Instantly share code, notes, and snippets.

@talegari
Last active December 29, 2016 07:56
Show Gist options
  • Save talegari/ad06da7795b8771e2e152f304ca00f6f to your computer and use it in GitHub Desktop.
Save talegari/ad06da7795b8771e2e152f304ca00f6f to your computer and use it in GitHub Desktop.
Functions to help with code execution analysis in R
# how much memory did an execution use in MBs
mem_usage <- function(...){
exprs <- as.list(match.call(expand.dots = FALSE)$...)
invisible(gc(reset = TRUE))
start_mem <- sum(gc()[,2])
lapply(exprs, eval, parent.frame())
max_mem <- sum(gc()[,6])
end_mem <- sum(gc()[,2])
return(list(final_mem_used = as.numeric(end_mem - start_mem)
, max_mem_used = as.numeric(max_mem - start_mem)
)
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment