简体   繁体   English

如何加快多处理python

[英]how to speed up multiprocessed python

I'm trying to generate a Mandelbrot Set image, but the image takes ages. 我正在尝试生成一个Mandelbrot Set图像,但是该图像需要很长时间。 The code is annoyingly slow, so If you can help me speed it up, that would be very helpful. 代码很烦人,所以如果您可以帮助我加快速度,那将非常有帮助。 thanks. 谢谢。 Note that code compiles and runs from my IDE. 请注意,代码是从我的IDE编译并运行的。 I am new to multiprocessing, so please dumb it down a bit for me. 我是多重处理的新手,所以请为我稍作调整。

def daemon_summoner(r):
'''allows code to be restarted with different file if crash during super high res generation'''
t = Thread(target=process_job(r))
t.start()


def process_job(r):
    """creates a slice of the set"""
    img = Image.new('1', size, color)  # B&W image
    print(int(r + 2*resolution100+1), 'of', int(resolution100 *3))
    r = r * 100
    for rval in range(100):
        for i in range(-resolution, resolution, 1):
            if abrot(((r + rval) / resolution), (i / resolution * 1j)):
                try:
                    img.putpixel((rval, i + resolution,), 0)
                except:
                    print(r, rval, i + resolution)
img.save(('z_images/' + str(resolution) + '/' + str(int(r/100)+int(resolution*.02)) + '.png'))


def abrot(x, y):
    """tests a point"""
    c = (x + y)
    z = 0 + 0j
    for _ in range(5):
        z = z * z + c
    if abs(real(z*z+c))>=2and abs(imag(z*z+c))>=2:
        return False
    for _ in range(int(resolution / 10)):
        if real(z + 0.0001) > float(real(z*z+c)) > real(z - 0.0001):
            return True
        z = z * z + c
        if abs(real(z)) >= 2:
            return False
    return True

Mandelbrot迭代z * z + c可能需要数十亿次操作才能生成单个图像,因此加快速度的第一步是删除iterate_once函数并在线完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM