zhanghaitao1
7/18/2018 - 12:51 AM

multithreading multiprocess

multithreading multiprocess

import os
import pandas as pd
from multiprocessing.dummy import Pool as ThreadPool
from config import DIR_DM_BACKTEST
import time
import multiprocessing

names=os.listdir(DIR_DM_BACKTEST)

def get_ret(name):
    return pd.read_csv(os.path.join(DIR_DM_BACKTEST,name,'hedged_returns.csv'),index_col=0,header=None).mean().values[0]

get_ret1=lambda name:pd.read_csv(os.path.join(DIR_DM_BACKTEST,name,'hedged_returns.csv'),index_col=0,header=None).mean().values[0]

if __name__ == '__main__':

    t1=time.time()
    pool=ThreadPool(10)
    results=pool.map(get_ret,names)
    t2=time.time()
    resuls1=[get_ret(name) for name in names]
    t3=time.time()

    poolProcess=multiprocessing.Pool(10)
    results2=poolProcess.map(get_ret,names)
    t4=time.time()


    print(t2-t1,t3-t2,t4-t3)
    #9.669275283813477 10.129716157913208 1.8918147087097168