R Under development (unstable) (2024-07-10 r86888 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > visualize <- FALSE # TRUE for manual tests with visualization of results > > if(visualize) + { + + library(parallel) + + + #fonction basique evaluant la moyenne empirique d'un echantillon gaussien + f <- function(i) mean(rnorm(1e4, mean=i)) + + #exemple simple + glist <- 1:20 + cl <- parallel::makeCluster(2) + system.time( + res <- parallel::parLapply(cl, glist, f) + ) + parallel::stopCluster(cl) + + + #exemple en faisant varier le nombre de coeurs et le nombre de simulations + nbsimu <- 10^(1:2) + cores <- 1:4 + cores <- 1:getOption("cl.cores", 2) + + partime <- matrix(NA, length(nbsimu), length(cores)+1) + colnames(partime) <- c("R", paste("core",cores)) + rownames(partime) <- paste("n", nbsimu, sep="=") + + partime[, 1] <- sapply(1:length(nbsimu), function(i) + system.time(lapply(1:nbsimu[i], f))[3]) + for(j in 1:length(cores)) + { + print(cores[j]) + cl <- parallel::makeCluster(cores[j]) + partime[, j+1] <- sapply(1:length(nbsimu), function(i) + system.time(parallel::parLapply(cl, 1:nbsimu[i], f))[3]) + parallel::stopCluster(cl) + } + + + partime + + } > > proc.time() user system elapsed 0.21 0.03 0.23