Skip to content

Instantly share code, notes, and snippets.

@LeeMendelowitz
Created March 7, 2015 18:14
Show Gist options
  • Save LeeMendelowitz/66f15528cdc008f41069 to your computer and use it in GitHub Desktop.
Save LeeMendelowitz/66f15528cdc008f41069 to your computer and use it in GitHub Desktop.
Time R Function Calls
############################################################
# Time a call. Execution time is printed. Return the value of wrapped call
time.call <- function(expr) {
start.time <- proc.time()
ret = evalq(expr)
end.time <- proc.time()
print(end.time - start.time)
ret
}
# Usage:
a = 10
b = time.call(runif(10000) + a)
# you can also wrap an assignment
time.call( c <- mean(runif(10000000) + a))
print(c)
########################################################
# Time a call to an already quoted expression
time.call.q <- function(expr) {
start.time <- proc.time()
ret = eval(expr)
end.time <- proc.time()
print(end.time - start.time)
ret
}
# You can use time.call.q to work on quoted expressions.
# Maybe this is more readable? Debatable. But lets you
# Easily "replace" the call to time.call.q with an eval
# if you keep changing your mind about whether to time the
# function call as you are developing.
library(magrittr)
d = quote(d <- mean(runif(10000000) + a) ) %>% time.call.q
e = quote(d <- mean(runif(10000000) + a) ) %>% eval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment