Google apps
Main menu

Post a Comment On: Only Python

"Profiling adventures and cython - Faster drawing and conclusion"

6 Comments -

1 – 6 of 6
Blogger Markus said...

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.

It might not be suitable for any apps but surely at least for some where simple stuff needs a heavy speedup

5:04 PM

Anonymous Ryan Ginstrom said...

Just from examining the generated C code, the following mandel function looks tighter because it avoid dipping into lots of CPython stuff.

@cython.boundscheck(False)
@cython.wraparound(False)
@cython.profile(False)
cdef inline int mandel(double real, double imag, int max_iterations=20):
'''determines if a point is in the Mandelbrot set based on deciding if,
after a maximum allowed number of iterations, the absolute value of
the resulting number is greater or equal to 2.'''
cdef double z_real = 0., z_imag = 0.
cdef int i
cdef double threshold = 4.

for i from 0 <= i < max_iterations:
z_real, z_imag = ( z_real*z_real - z_imag*z_imag + real,
2*z_real*z_imag + imag )
if (z_real*z_real + z_imag*z_imag) >= threshold:
return i
return -1

You could likely do something similar for the rest. There are good hints for speeding up code with cython in the SciPy 2009 proceedings:
http://conference.scipy.org/proceedings/SciPy2009/

8:26 PM

Blogger André Roberge said...

Ryan:
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.

8:56 PM

Anonymous Anonymous said...

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.

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.

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.

-Chris Barker

4:08 PM

Anonymous Anonymous said...

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.

I wrote a Wiki page about it in teh Cython Wiki:

http://wiki.cython.org/examples/mandelbrot

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.
-Chris Barker

8:39 PM

Blogger André Roberge said...

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).

8:49 PM

Spammers: none shall pass.
You can use some HTML tags, such as <b>, <i>, <a>

Comments on this blog are restricted to team members.

You will be asked to sign in after submitting your comment.
Please prove you're not a robot