Skip to content

Instantly share code, notes, and snippets.

@dsparks
Created February 15, 2011 00:03
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 dsparks/826841 to your computer and use it in GitHub Desktop.
Save dsparks/826841 to your computer and use it in GitHub Desktop.
Example of parallel vectorized functions using snow
require(snow)
VariabletoExport <- 5
TheFunction <- function(x){
TimeWaster <- mean(rnorm(100000, 0, 1))
x^2 + TimeWaster + VariabletoExport
}
FunctionInput <- 1:1000
### Unparallelized ###
StartTime <- Sys.time()
FunctionOutput <- lapply(FunctionInput, TheFunction)
ProcessTime <- Sys.time() - StartTime
ProcessTime
### Parallelized ###
StartTime <- Sys.time()
TheCluster <- makeCluster(8, type = "SOCK")
clusterSetupRNG(TheCluster, type = "RNGstream")
clusterExport(TheCluster, c("VariabletoExport"))
FunctionOutput <- parSapply(TheCluster, FunctionInput, TheFunction)
stopCluster(TheCluster)
ProcessTime <- Sys.time() - StartTime
ProcessTime
@dsparks
Copy link
Author

dsparks commented Feb 15, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment