nerdlyfe
nerdlyfe

Reputation: 527

R no performance difference between lapply and mclapply

Hello I'm attempting to use the R package parallel, and specifically the mclapply() function to use my 8 core system to generate a moving average over 10000 columns and 24 rows (moving average over the columns, thus 24 data points are being smoothed over, 10000 times).

However, when I compare the performance speed of standard lapply to mclapply, I see no speed up. Could someone explain why I see no difference in run time?

> x = replicate(10000, rnorm(24)) 
> system.time({
+   tmpp = lapply(list(x), function(x) {
+     rollmean(x, 7)
+   })
+ })
user  system elapsed 
15.309   7.893  23.201 
> 
> detectCores() 
[1] 8
> 
> system.time({
+   tmpp = mclapply(list(x),mc.cores=8, function(x) {
+     rollmean(x, 7)
+   })
+ })
user  system elapsed 
15.628   7.948  23.573 

Upvotes: 1

Views: 2365

Answers (1)

IRTFM
IRTFM

Reputation: 263332

You are giving a single list, so no parallelism:

 x <-replicate(10000, list(rnorm(24)) )  # list length 10000 
 library(zoo)
 library(parallel)
 system.time({
    tmpp = lapply(x, function(x) {
      rollmean(x, 7)
    })
  })
#   user  system elapsed 
#  8.250   0.044   8.249 
 parallel::detectCores()
#[1] 8
 system.time({
    tmpp = mclapply(x, mc.cores=8, function(z) {
      rollmean(z, 7)
    })
  })
#   user  system elapsed 
# 14.376   8.858   3.922 

(This was on a 6 year-old MacPro.)

Upvotes: 2

Related Questions