Parallelize python code with R
Let this python code to be parallelized:
import time
def basic_func(x):
if x == 0:
return 'zero'
elif x%2 == 0:
return 'even'
else:
return 'odd'
starttime = time.time()
for i in range(0,10):
y = i*i
# time.sleep(2)
print('{} squared results in a/an {} number'.format(i, basic_func(y)))
print('That took {} seconds'.format(time.time() - starttime))
Do this in R:
library(microbenchmark)
library(parallel)
microbenchmark({out1 <- parallel::mclapply(1:1000, FUN=function(x) {system("python3 essai.py", intern=TRUE)}, mc.cores = detectCores())}, times = 1L)
microbenchmark({out1 <- lapply(1:1000, FUN=function(x) {system("python3 essai.py", intern=TRUE)})}, times = 1L)
import time
def basic_func(x):
if x == 0:
return 'zero'
elif x%2 == 0:
return 'even'
else:
return 'odd'
starttime = time.time()
for i in range(0,10):
y = i*i
# time.sleep(2)
print('{} squared results in a/an {} number'.format(i, basic_func(y)))
print('That took {} seconds'.format(time.time() - starttime))
library(microbenchmark)
library(parallel)
microbenchmark({out1 <- parallel::mclapply(1:1000, FUN=function(x) {system("python3 essai.py", intern=TRUE)}, mc.cores = detectCores())}, times = 1L)
microbenchmark({out1 <- lapply(1:1000, FUN=function(x) {system("python3 essai.py", intern=TRUE)})}, times = 1L)
Commentaires
Enregistrer un commentaire