python for并行方法

import SimpleITK as sitk
import numpy as np
import pandas as pd
from pandas import DataFrame
import multiprocessing
from multiprocessing import Pool
import time
import datetime
import timeit
import itertools
file = r'D:\project\gyw\1.nii.gz'
image = sitk.ReadImage(file)
data = sitk.GetArrayFromImage(image)
def getvalue_data(item):
    temp = data[:, item[2], item[1], item[0]]
    return temp

if __name__ == '__main__':
    start = datetime.datetime.now()
    (a, b, c, d) = np.shape(data)  # a为时间,b为层数,c为coronal(y轴),d为sagottal(x轴)
    # print(a,b,c,d)  # (50, 20, 256, 256)
    dftime = pd.DataFrame({'Time': []})
    p = multiprocessing.Pool(6)
    items_x = [x for x in range(0, 10)]
    items_y = [x for x in range(0, 10)]
    items_z = [x for x in range(0, 10)]
    paramlist = list(itertools.product(items_x,items_y,items_z))
    start = timeit.default_timer()
    for x in range(0, 10):
        for y in range(0, 10):
            for z in range(0, 10):
                print('processing', x, y, z)
                temp = data[:, z, y, x]
                dftime[(x, y, z)] = temp  # 增加新列
    end = timeit.default_timer()
    print('single processing time:', str(end - start), 's')
    start = timeit.default_timer()
    b = p.map(getvalue_data,paramlist)
    p.close()
    p.join()
    end = timeit.default_timer()
    print('multi processing time:', str(end - start),'s')
    dftime = pd.DataFrame(b).T

数据下载百度网盘
提取码:49rs

上一篇:python的list费时对比


下一篇:【python程序的性能优化总结】