Created
December 23, 2013 13:37
-
-
Save Malarkey73/8097258 to your computer and use it in GitHub Desktop.
R Vectorisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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