Instantly share code, notes, and snippets.

Embed
What would you like to do?
R code used for the benchmark of child processes created by pbapply/pbmcapply.
library(data.table)
library(pbapply)
library(pbmcapply)
# A lazy sqrt function which doesn't care about efficiency.
# It is derived from the testcases of pbmcapply package.
# It allows the program to run for a prolonged process so that
# we can monitor the forking process.
lazySqrt <- function(num) {
# Sleep randomly between 0 to 1 second
Sys.sleep(runif(1))
return(sqrt(num))
}
timeConsumedPbapply <- data.table()
timeConsumedPbmcapply <- data.table()
numCores <- 4L
repeats <- 1
maxNum <- 30
FUN <- lazySqrt
cat("Start benchmarking with pbapply in 5 seconds.\n")
Sys.sleep(5)
# Check for pbapply
for(index in 1:repeats) {
time <- system.time(sqrt <- pblapply(1:maxNum, FUN, cl = numCores))
}
cat("Start benchmarking with pbmcapply in 5 seconds.\n")
Sys.sleep(5)
# Check for pbmcapply
for(index in 1:repeats) {
time <- system.time(sqrt <- pbmclapply(1:maxNum, FUN, mc.cores = numCores))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment