如何使我的曼德布洛特绘图仪更快



如何让我的 Python 程序更快?该程序计算曼德布洛特集合并用绘制它。我认为问题出在 for 循环中。也许这些步骤花费了太多时间。

import numpy as np
import turtle
turtle.ht()
turtle.pu()
turtle.speed(0)
turtle.delay(0) turtle.colormode(255)
i= int(input("iteration = "))
g = int(input("accuracy  = "))
xmin = float(input("X-min: "))
xmax = float(input("X-max: "))
ymin = float(input("Y-min: "))
ymax = float(input("Y-max: "))
cmode = int(255/i)
input("PRESS TO START")
for x in np.arange(xmin,xmax,1/g):
    for y in np.arange(ymin,ymax,1/g):
        c = x + y * 1j
        z = 0
        t = 1
        for e in range(i):
            z = z * z + c
            if abs(z) > 3:
                turtle.setx(g*c.real)
                turtle.sety(g*c.imag)
                turtle.dot(2,e*cmode,e*cmode,e*cmode)
                t = 0
        if t == 1:
            turtle.setx(g*c.real)
            turtle.sety(g*c.imag)
            turtle.dot(2,"black")
input("Calculated!")
turtle.mainloop()

这是一个例子

以下返工应该比原始返工快一百倍:

import numpy as np
import turtle
i = int(input("iteration = "))
g = int(input("accuracy  = "))
xmin = float(input("X-min: "))
xmax = float(input("X-max: "))
ymin = float(input("Y-min: "))
ymax = float(input("Y-max: "))
cmode = int(255 / i)
input("PRESS TO START")
turtle.hideturtle()
turtle.penup()
turtle.speed('fastest')
turtle.colormode(255)
turtle.setundobuffer(None)  # turn off saving undo information
turtle.tracer(0, 0)
for x in np.arange(xmin, xmax, 1 / g):
    for y in np.arange(ymin, ymax, 1 / g):
        c = x + y * 1j
        z = 0
        t = True
        for e in range(i):
            z = z * z + c
            if abs(z) > 3.0:
                turtle.setposition(g * c.real, g * c.imag)
                rgb = e * cmode
                turtle.dot(2, rgb, rgb, rgb)
                t = False
                break
        if t:
            turtle.setposition(g * c.real, g * c.imag)
            turtle.dot(2, "black")
    turtle.update()
print("Calculated!")
turtle.mainloop()

重大变化是使用 tracer()update() 的组合,以避免直观地为用户绘制每个点,而只是在每个垂直列完成时进行绘制。

最新更新