[Tutor] learning to program in cython
Oscar Benjamin
oscar.j.benjamin at gmail.com
Thu Jan 17 00:03:54 CET 2013
On 16 January 2013 21:54, Abhishek Pratap <abhishek.vit at gmail.com> wrote:
> Hi Guys
>
> With the help of an awesome python community I have been able to pick up the
> language and now willing to explore other cool extensions of it.
Good work!
>
> I routinely have large loops which could be ported to cython for speed.
> However I have never written a single line of cython code. Any pointers on
> getting started.
There are two reasons for using cython:
1) To interface directly with existing C libraries.
2) To speed up CPU-intensive code.
It sounds like you're interested in case 2). However, not all loops
benefit from cythonisation. Loosely cython is good when
a) you're doing something that can be written in a small amount of
efficient C code
b) but the corresponding Python code involves a lot of repeated
function calls or expression evaluations.
If you're already familiar with C then you'll probably have some idea
when a) and b) apply. I would say that a prerequisite for learning to
speed up CPU-intensive code with cython would be learning to use the
python profilers. In particular you should learn to use cProfile and
timeit:
http://docs.python.org/2/library/profile.html
http://docs.python.org/2/library/timeit.html
As with any optimisation it is important to study the performance of
your code and identify bottlenecks first. It is also generally true
that you should (at least) consider algorithmic optimisation before
you consider using something like cython for micro-optimisation. One
of the guiding principles in the design of the Python language is that
big-O complexity is often more important than micro-optimisation.
Similarly, you should normally try to optimise and benchmark your code
in pure Python before attempting to improve on that with cython.
>
> A tutorial text or video would be of great help.
Here is the official "basic tutorial" for cython:
http://docs.cython.org/src/userguide/tutorial.html
There are also lots of helpful people on the cython-users mailing list:
https://groups.google.com/forum/?fromgroups#!forum/cython-users
If you have a particular problem in mind that might benefit from
cythonisation, then why not post (a shortened but complete version of)
it here to see if anyone has any pointers about how to improve the
performance with/without cython.
Oscar
More information about the Tutor
mailing list