Skip to content

Instantly share code, notes, and snippets.

@ldaniel
Last active December 3, 2019 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ldaniel/632ff4049dc83e54f8c12fe2ddea4a67 to your computer and use it in GitHub Desktop.
Save ldaniel/632ff4049dc83e54f8c12fe2ddea4a67 to your computer and use it in GitHub Desktop.
install.packages("rbenchmark")
library(compiler)
library(rbenchmark)
enableJIT(level = 3)
#enableJIT(level = 0)
my_mean = function(x) {
total = 0
n = length(x)
for(i in 1:n)
total = total + x[i]/n
total
}
cmp_mean = cmpfun(my_mean)
x = rnorm(100)
benchmark(my_mean(x), cmp_mean(x), mean(x),
columns=c("test", "elapsed", "relative"),
order="relative", replications=5000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment