How to increase the running efficiency by 17 times with Python code
In this issue, the editor will bring you about how to improve the running efficiency by 17 times with Python code. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Mandelbrot set is an interesting mathematical phenomenon involving bit operations, recursion and imaginary numbers. Because it is a complex and diverse function, it is a very good case study on how to improve the running efficiency of the code.
By timing the function mandelbrot_set, we find that it takes an average of 8 seconds to complete.
Import numpy as np def mandelbrot_set (width, height, zoom=1, x_off=0, y_off=0, niter=256): h = width, height pixels = np.arange (wigh, dtype=np.uint16) .reshape (h) W) for x in range (w): for y in range (h): zx = 1.5* (x + x_off-3*w/4) / (0.5*zoom*w) zy = 1.0* (y + y_off-hmax 2) / (0.5*zoom*h) z = complex (zx, zy) c = complex (0 0) for i in range (niter): if abs (c) > 4: break c = cymbals 2 + z color = (I