Skip to content

Instantly share code, notes, and snippets.

@kvnkuang
Created November 19, 2016 15:52
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 kvnkuang/884ac81f5e7d0ff3e262216a230bdc61 to your computer and use it in GitHub Desktop.
Save kvnkuang/884ac81f5e7d0ff3e262216a230bdc61 to your computer and use it in GitHub Desktop.
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