2

I am employing an embarassingly parallel routine using mclapply() of the parallel package in R in order to simulate independent paths of a stochastic process. I am surprised that I could not get any speed gain over the non-parallel program, though.

Is there any task which is established to be performed faster in parallel than on a single core which I can use to check whether the system is working as expected?

Peter H.
  • 1,995
  • 8
  • 26
Mr Frog
  • 296
  • 2
  • 16
  • The answer already posted is good. See also [my answer to this question](https://stackoverflow.com/questions/74706306/how-do-i-parallelize-this-lapply-function-in-r/74709053#74709053) for some examples where parallel processing is not faster than a single thread. – SamR Feb 03 '23 at 12:11

1 Answers1

1

you could try executing

Sys.sleep(5)

which suspends the session for 5 seconds. If the parallelisation works, then each of the worker processes should be sleeping at the same time.


library(parallel)
library(tictoc)

f <- function(...) {
  Sys.sleep(1) 
  "DONE"
}

tic()
res <- lapply(1:5, f)
toc()
#> 5.025 sec elapsed

tic()
res <- mclapply(1:5, f, mc.cores = 5)
toc()
#> 1.019 sec elapsed

Created on 2023-02-03 with reprex v2.0.2

Peter H.
  • 1,995
  • 8
  • 26