如何讓我的Python程序更快?這個程序計算Mandelbrot集並用龜畫出它。我認爲問題出現在for循環中。也許這些步驟花費了太多時間。如何讓我的Mandelbrot繪圖儀更快?
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()
有關性能的問題更有可能在[代碼審查](https://codereview.stackexchange.com/)上得到解答。 – Badda
謝謝!我是一位新用戶,非常感謝您的幫助。不知道。 @Badda – akinoioi
第一次加速是在't = 0'後面添加'break'(如果繪製例程被觸發,則不需要爲此點繼續計算)。除此之外,Python並不是非常快;您可以嘗試將計算推入Numpy中,如https://thesamovar.wordpress.com/2009/03/22/fast-fractals-with-python-and-numpy/(聲稱速度提高3倍),或者您可以切換到一個像Julia https://julialang.org/這樣更快的編譯語言(甚至這個名字是合適的;-),並且它們包含mandelbrot上的20倍加速作爲他們的基準之一) –