Benchmark the performance of pbapply and pbmcapply.
library(data.table) | |
library(pbapply) | |
library(pbmcapply) | |
timeConsumedPbapply <- data.table() | |
timeConsumedPbmcapply <- data.table() | |
timeConsumedapply <- data.table() | |
numCores <- 4L | |
repeats <- 50 | |
maxNum <- 10 | |
FUN <- sqrt | |
cat("Start benchmarking with pbapply in 5 seconds.\n") | |
Sys.sleep(5) | |
# Check for pbapply | |
for(index in 1:repeats) { | |
time <- system.time(sqrtRes <- pblapply(1:maxNum, FUN, cl = numCores)) | |
timeConsumedPbapply <- rbind(timeConsumedPbapply, as.data.table(t(as.matrix(time)))) | |
} | |
cat("Start benchmarking with pbmcapply in 5 seconds.\n") | |
Sys.sleep(5) | |
# Check for pbmcapply | |
for(index in 1:repeats) { | |
time <- system.time(sqrtRes <- pbmclapply(1:maxNum, FUN, mc.cores = numCores)) | |
timeConsumedPbmcapply <- rbind(timeConsumedPbmcapply, as.data.table(t(as.matrix(time)))) | |
} | |
# Use lapply as reference | |
for(index in 1:repeats) { | |
time <- system.time(sqrtRes <- lapply(1:maxNum, FUN)) | |
timeConsumedapply <- rbind(timeConsumedapply, as.data.table(t(as.matrix(time)))) | |
} | |
cat("\nResutls:\n") | |
colMeans(timeConsumedPbapply) | |
colMeans(timeConsumedPbmcapply) | |
colMeans(timeConsumedapply) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment