Re: What code speeds up the most when Cythonized?

143 views
Skip to first unread message
Message has been deleted

Björn Dahlgren

unread,
May 25, 2013, 7:56:01 AM5/25/13
to cython...@googlegroups.com
You should have a look at:
http://docs.cython.org/src/tutorial/profiling_tutorial.html

I usually get numbers like yours (i.e. ~20x speedup) when I naively just add type information to my old python code.
Usually that's enough to make something else the bottle-neck, if not you might want to look into how many intermediate
python objects you are creating and work on C arrays instead. (either by writing Cython or writing a separate C routine and interfacing)

Hope it helps!
/Björn

On Thursday, 23 May 2013 19:14:55 UTC+2, Zak wrote:
Hello Cython List,

I am curious, what code speeds up the most when Cythonized? A specific code example would be good. I think I am using the word Cythonize in the accepted fashion, but I am new to the community. By Cythonize, I mean converting .py files into .pyx files and then adding static type declarations to the code.

I am just curious, I want to see what speeds up the most, and thus perhaps gain a better understanding of how CPython and Cython each work internally.

How much faster have your projects gotten? I have done two projects in Cython. One project was statistical cross-correlation, but I don't know the speed-up factor because I wrote it in Cython from the beginning. The other project was numerical approximation, it had a 16x speed-up when I used "except? -1" in my cdef functions and a 20x speedup when I omitted exception handling altogether. I was disappointed to see only 20x speed-up.

The Cython documentation gives an example of numerically integrating a quadratic equation and it says there is a 150x speed-up. I have not tried compiling and running their example, I am hoping you can post examples with more extreme speed-up ratios.

Thank you,

Zak

Sturla Molden

unread,
May 25, 2013, 1:30:49 PM5/25/13
to cython...@googlegroups.com

Den 23. mai 2013 kl. 19:14 skrev Zak <cyt...@m.allo.ws>:


I am curious, what code speeds up the most when Cythonized?

Algorithmic or numerical code that cannot be solved using performance libraries (e.g. Intel MKL) or expressed as vectorized high-level statements (e.g. with numpy, but not only that).


A specific code example would be good.

This is an example of code where Cython helps enormously:



Ok granted: The Cython code would have used memoryviews if we write it today. 
But it was written for a version SciPy that needed to maintain Python 2.4 compatibility.


How much faster have your projects gotten?

It depends on what you do. In my experience, as a rule of thumb:

- Normal Python code to Cython: 2 to 4x improvement
- NumPy code to Cython: 1 to 20x
- Text processing: 1 to 20x
- MKL functions in NumPy, etc: Cython slower
- Plain algorithmic Python code to Cython: 100 to 2000x

3d graphics:
- OpenGL, vertex arrays/buffers, and NumPy: Similar
- OpenGL, GSL: Similar
- OpenGL, display lists: Similar
- OpenGL, old-style glVertex4* et al.: 1000x or better

However: It is not just about speed! Cython is (in my opinion) the easiest 
way to write C or C++ extensions for Python. E.g. if you have a C library
you want to use from Cython. One example is the Python wrapper for ZMQ.
Cython is also an alernative to Python CAPI, swig, boost.python, ctypes or CXX!



One example


Sturla Molden

unread,
May 25, 2013, 1:51:49 PM5/25/13
to cython...@googlegroups.com

Den 23. mai 2013 kl. 19:14 skrev Zak <cyt...@m.allo.ws>:

>
> The Cython documentation gives an example of numerically integrating a quadratic equation and it says there is a 150x speed-up. I have not tried compiling and running their example, I am hoping you can post examples with more extreme speed-up ratios.
>

When we timed the performance of scipy.spatial.cKDTree, it was sometimes 1500x times faster than scipy.spatial.kdtree (its Python prototype). But that is an extreme case.

A similar situation is when we write 3D graphics in "old-style" OpenGL and call glVertex4f millions of times in tight loops. Putting those loops into Cython will help enormously. (But graphics wizards will tell you to use vertex arrays and VBOs instead, in which case Cython doesn't help a lot.)

BTW: The clue to using Cython efficiently is knowing when to NOT use it! Until you get a feel for it, start by writing Python, and let the profiler tell you the locations of the bottlenecks. Don't touch Cython until you have a wrorking prototype. If the Python code is "fast enough", don't bother to optimize because it is already fast enough (faster than "fast enough" is still just fast enough). If the Python code is "too slow", use the profiler to locate the hotspots. They might not be where you think! Then check if the bottleneck involves call to a C extension, and if the overhead of the Python function call is comparably small. If the answer to either is no, you have a candidate region that might benefit from some Cython :-)

Sturla





Gelin Yan

unread,
May 25, 2013, 1:50:39 PM5/25/13
to cython...@googlegroups.com

--
 
---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Hi 

  I am interested in seeing some examples about how cython can improve text processing. In this area, I didn't find too much info on internet. Thanks

Regards

gelin yan

Lars Buitinck

unread,
Jun 2, 2013, 4:54:17 AM6/2/13
to cython...@googlegroups.com
2013/5/25 Gelin Yan <dyna...@gmail.com>:
> I am interested in seeing some examples about how cython can improve text
> processing. In this area, I didn't find too much info on internet. Thanks

In scikit-learn, we use Cython to parse SVMlight-format text files.
The parser mostly runs Python functions, but does a few things using C
functions for a significant speedup.

https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/datasets/_svmlight_format.pyx

--
Lars Buitinck
Scientific programmer, ILPS
University of Amsterdam
Reply all
Reply to author
Forward
0 new messages