def test_cython(): x1, x2, y1, y2, maxiter = TESTS try: import mandel xx = nm.arange(x1, x2, (x2-x1)/w*2, dtype=nm.float32) yy = nm.arange(y2, y1, (y1-y2)/h*2, dtype=nm.float32) time_start = time.time() res = mandel.generate_mandelbrot(xx, yy, maxiter) time_end = time.time() cython_secs = time_end - time_start print "Cython took ", cython_secs fun = calculate_z_numpy x1, x2, y1, y2, maxiter = TESTS res2 = test_mandelbrot(fun, x1, x2, y1, y2, maxiter, True) print "Debug info" print res.shape, res.dtype print res.ravel()[:100] print res2.shape, res2.dtype print res2[:100] print abs(1.0 * nm.sum(res - res2.reshape((len(xx),len(yy)))) / nm.sum(res)) * 100 print nm.sum(res == -1) assert_true(nm.allclose(res, res2.reshape((len(xx),len(yy))))) except ImportError: print "You must compile the Cython extension"
4. Turn the mandelbrot_escape function into a C only function. Re-compile, and see how much of a speed-up you get. Bonus ~~~~~ Use the numpy Cython interface to optimize the code even further. Re-compile, and see how much of a speed-up you get. """ # This generates a hi-res version of the Mandelbrot set... only run on a # fast version of the cython code unless you have a lot of time. if __name__ == '__main__': import time from numpy import vectorize, r_, c_ from matplotlib.pyplot import imshow, show, cm, savefig from mandel import generate_mandelbrot x = r_[-2:1:2000j] y = r_[-1.5:1.5:2000j] t = time.time() d = generate_mandelbrot(x, y,100) print 'Execution time:', time.time() - t imshow(d, extent=[-2,1,-1.5,1.5], cmap=cm.gist_stern) show()
def _get_img(self): return generate_mandelbrot(self.xs, self.ys, self.lod)
3. Add variable typing for the scalar variables in the mandelbrot.pyx file. Re-compile, and see how much of a speed-up you get. 4. Turn the mandelbrot_escape function into a C only function. Re-compile, and see how much of a speed-up you get. Bonus ~~~~~ Use the numpy Cython interface to optimize the code even further. Re-compile, and see how much of a speed-up you get. """ if __name__ == '__main__': import time from numpy import vectorize, r_, c_ from matplotlib.pyplot import imshow, show, cm from mandel import generate_mandelbrot x = r_[-2:1:100j] y = r_[-1.5:1.5:100j] t = time.time() d = generate_mandelbrot(x, y, 100) print 'Execution time:', time.time() - t imshow(d, extent=[-2,1,-1.5,1.5], cmap=cm.gist_stern) show()
def _get_mandel_img(self): x = linspace(self.x_low, self.x_high, self.resolution) y = linspace(self.y_low, self.y_high, self.resolution) return generate_mandelbrot(x, y, self.detail)