Created
July 19, 2020 03:17
-
-
Save franzbischoff/68a5c355e639c2b94d2c21564eb42fd8 to your computer and use it in GitHub Desktop.
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
#Load Libraries | |
library(foreach) | |
library(utils) | |
library(iterators) | |
library(doParallel) | |
#Choose number of iterations | |
n <- 1000 | |
#Progress combine function | |
f <- function(){ | |
pb <- txtProgressBar(min=1, max=n-1,style=3) | |
count <- 0 | |
function(...) { | |
count <<- count + length(list(...)) - 1 | |
setTxtProgressBar(pb,count) | |
Sys.sleep(0.01) | |
flush.console() | |
c(...) | |
} | |
} | |
#Start a cluster | |
cl <- makeCluster(4, type='SOCK') | |
registerDoParallel(cl) | |
# Run the loop in parallel | |
k <- foreach(i = icount(n), .combine=f()) %dopar% { | |
log2(i) | |
} | |
head(k) | |
#Stop the cluster | |
stopCluster(cl) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Possible slow