Skip to content

Instantly share code, notes, and snippets.

@franzbischoff
Created July 19, 2020 03:17
Show Gist options
  • Save franzbischoff/68a5c355e639c2b94d2c21564eb42fd8 to your computer and use it in GitHub Desktop.
Save franzbischoff/68a5c355e639c2b94d2c21564eb42fd8 to your computer and use it in GitHub Desktop.
#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)
@franzbischoff
Copy link
Author

Possible slow

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