Skip to content

Instantly share code, notes, and snippets.

@Malarkey73
Created December 23, 2013 13:37
Show Gist options
  • Save Malarkey73/8097258 to your computer and use it in GitHub Desktop.
Save Malarkey73/8097258 to your computer and use it in GitHub Desktop.
R Vectorisation
# vectorised function
vectorised <- function(x, y)
{
r = exp(-abs(x-y))
return(r)
}
#devectorised function
devectorised <- function(x,y)
{
r=rep(NA, length(x))
for(i in seq_along(x))
{
r[i]= exp(-abs(x[i]-y[i]))
}
return(r)
}
# data
x= 1:2e6
y= x * 2
# tldr RESULTS !!!!!!!!!!!!!!!!!!
library(microbenchmark)
microbenchmark(
vectorised(x,y),
devectorised(x,y),
unit = "s", times=5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment