把循环改成并行可以显著提升代码运行效率
R语言
借助furrr和future包
WORKER_NUM = 10
future::plan(future::multisession, workers = WORKER_NUM)
furrr::future_walk(elementlist, function(ele){...})
Python
借助concurrent.futures包
把需要遍历的元素放在 element_list 里面
import sys, concurrent.futures
WORKER_NUM = 10
def function_for_each_element(ele):
# Defines your function here
with concurrent.futures.ProcessPoolExecutor(max_workers=WORKER_NUM) as executor:
executor.map(function_for_each_element, element_list)