tag:blogger.com,1999:blog-9266717.post5352438622863189360..comments2023-05-22T10:01:23.167-03:00Comments on Only Python: Profiling adventures and cython - Faster drawing and conclusionAndré Robergehttp://www.blogger.com/profile/08131391818998844540noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-9266717.post-27760050950991558792010-02-04T20:49:08.351-04:002010-02-04T20:49:08.351-04:00Chris: Thanks for doing this. I saw your post on...Chris: Thanks for doing this. I saw your post on the cython users list. Unfortunately, I can't see the cython wiki - it is denying any request to see it. Hopefully it won't do the same to others (if so, look up the cython-users list).André Robergehttps://www.blogger.com/profile/08131391818998844540noreply@blogger.comtag:blogger.com,1999:blog-9266717.post-45074875765142507492010-02-04T20:39:51.393-04:002010-02-04T20:39:51.393-04:00OK -- I was thinking about it, so I did it. I wrot...OK -- I was thinking about it, so I did it. I wrote a version that uses a numpy array to create and store the image, and then, since I'm familiar with wx, I displayed it in a wx app. <br /><br />I wrote a Wiki page about it in teh Cython Wiki:<br /><br />http://wiki.cython.org/examples/mandelbrot<br /><br />I never got your TK version working on my machine, so I don't know how it compares, but I think I got the image drawing bit down to totally negligible.<br /> -Chris BarkerAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9266717.post-87477197539319429872010-02-01T16:08:11.856-04:002010-02-01T16:08:11.856-04:00You might be able to speed up the drawing quite a ...You might be able to speed up the drawing quite a bit by getting rid of the drawing calls. Instead, you can use an array to store the image, and alter it pixel by pixel with Cython.<br /><br />I'd use a numpy array to do it, but if you don't want to use any external libs, you can use cython to create a regular old C array, and use that to store your image.<br /><br />I'm not sure how to get an array into an image with Tk to display it, but I'll bet it can be done.<br /><br />-Chris BarkerAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-9266717.post-46154574047476888422010-01-17T20:56:31.455-04:002010-01-17T20:56:31.455-04:00Ryan:
Thanks for the suggestion and the link. ...Ryan:<br /> Thanks for the suggestion and the link. I tried to make this change with the latest version but did not notice any speedup. I will read the papers you linked to and see if I can make some improvements.André Robergehttps://www.blogger.com/profile/08131391818998844540noreply@blogger.comtag:blogger.com,1999:blog-9266717.post-25380576878188870602010-01-17T20:26:44.506-04:002010-01-17T20:26:44.506-04:00Just from examining the generated C code, the foll...Just from examining the generated C code, the following mandel function looks tighter because it avoid dipping into lots of CPython stuff.<br /><br />@cython.boundscheck(False)<br />@cython.wraparound(False)<br />@cython.profile(False)<br />cdef inline int mandel(double real, double imag, int max_iterations=20):<br /> '''determines if a point is in the Mandelbrot set based on deciding if,<br /> after a maximum allowed number of iterations, the absolute value of<br /> the resulting number is greater or equal to 2.'''<br /> cdef double z_real = 0., z_imag = 0.<br /> cdef int i<br /> cdef double threshold = 4.<br /><br /> for i from 0 <= i < max_iterations:<br /> z_real, z_imag = ( z_real*z_real - z_imag*z_imag + real,<br /> 2*z_real*z_imag + imag )<br /> if (z_real*z_real + z_imag*z_imag) >= threshold:<br /> return i<br /> return -1<br /><br />You could likely do something similar for the rest. There are good hints for speeding up code with cython in the SciPy 2009 proceedings:<br />http://conference.scipy.org/proceedings/SciPy2009/Ryan Ginstromhttp://ginstrom.com/scribbles/noreply@blogger.comtag:blogger.com,1999:blog-9266717.post-85139460380997429742010-01-17T17:04:06.004-04:002010-01-17T17:04:06.004-04:00Nice post series, thanks for the quick dive into c...Nice post series, thanks for the quick dive into cython. Seems nice all in all. Acknowledging that hand-written algos might be more efficient, yet the time you needed to get it going that fast was incredibly short. A major +1 for cython imho.<br /><br />It might not be suitable for any apps but surely at least for some where simple stuff needs a heavy speedupMarkushttps://www.blogger.com/profile/10378437611717776110noreply@blogger.com