首页 > 解决方案 > 多线程/进程加速双循环内的函数?

问题描述

我正在尝试做这样的事情:

import skimage  

def dostuff(im, [m, n]):
    # im is a 2D numpy array greyscale image.
    # This calculate the line profile from a given point [m,n] 
    # to the origin [0, 0] on the im
    line = skimage.measure.profile_line(im, [m,n], [0,0])
    first_two = line[:2]
    return first_two 

x = 20
matrix = np.zeros(2*x+1, 2*x+1, 1000) 
for m in range(-x, x+1):
    for n in range(-x, x+1):
        result = dostuff(im, [m,n])
        matrix[m, n] = result

有什么方法可以通过在 python 中使用多线程或多处理来加速这种双循环计算?

标签: pythonmultithreadingnumpyfor-loopmultiprocessing

解决方案


推荐阅读