Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- as <- runif(30) # limit for
- bs <- runif(30)
- cs <- runif(30) # distribution of food sources ('random'/'clustered')
- p_space <- list ()
- for (a in as){
- for (b in bs){
- for (c in cs){
- p_space[[length(p_space)+1]] <- c(a,b,c)
- }
- }
- }
- made_up <- function(pvec){
- a <- pvec[1]
- b <- pvec[2]
- c <- pvec[3]
- for(i in 1:10000){
- d <- a*b*c
- }
- return(list(a,b,c,d))
- }
- system.time({
- z <- lapply(p_space,made_up)
- results1 <- data.frame(matrix(unlist(z),ncol = 4, byrow = T))
- colnames(results1) <- c('a','b','c','product')
- })
- user system elapsed
- 21.23 0.01 21.26
- library(parallel)
- no_cores <- detectCores()
- cl <- makeCluster(no_cores)
- clusterExport(cl, c("p_space"))
- # clusterEvalQ() #for libraries on all nodes
- system.time ({
- z <- parLapply(cl, p_space, made_up)
- results <- data.frame(matrix(unlist(z),ncol = 4, byrow = T))
- colnames(results) <- c('a','b','c','product')
- })
- user system elapsed
- 0.02 0.02 10.86
Add Comment
Please, Sign In to add comment