views:

593

answers:

4

I know a bunch of scripting languages, (python, ruby, lua, php) but I don't know any compiled languages like C/C++ , I wanted to try and speed up some python code using cython, which is essentially a python -> C compiler, aimed at creating C extensions for python. Basically you code in a stricter version of python which compiles into C -> native code.

here's the problem, I don't know C, yet the cython documentation is aimed at people who obviously already know C (nothing is explained, only presented), and is of no help to me, I need to know if there are any good cython tutorials aimed at python programmers, or if I'm gonna have to learn C before I learn Cython.

bear in mind I'm a competent python programmer, i would much rather learn cython from the perspective of the language I'm already good at, rather than learn a whole new language in order to learn cython.

1) PLEASE don't recommend psyco

edit: ANY information that will help understand the oficial cython docs is useful information

+5  A: 

cython is good at two different things

  1. Interfacing with C language libraries
  2. Speeding up Python code

It probably gets more exposure from 1. hence the emphasis on the tutorial materials you've found towards C stuff. It sounds like you want to use it like 2. though.

From my experience with cython you can just try compiling your python programs and see if it works. It will get a bit faster (maybe). To get a lot faster you need to selectively turn python types into C types. This starts to bring out the power of cython.

If you look at the official tutorial you need to study where they've used the cdef keyword.

So to recap

  1. Make your existing python program compile with cython with as few changes as possible
  2. Declare some variables as cdef and make it work again
  3. If not fast enough go to step 2.

I'm sorry that isn't a pointer to a tutorial, but it should give you a direction to go in!

Nick Craig-Wood
thanks a lot, i also wondered if maybe there's a C tutorial on the basic structure of C that will help understand the offical cython docs better
spearfire
+2  A: 

Learn C! (Sorry -- irresistible.)

Seriously, though, it seems like you mostly need to know about C variable types (C types, if you will) in order to use cdef effectively.

Later on, if you do decide to bite the bullet and learn C properly, treat yourself to a copy of Kernighan and Ritchie, or K & R, available on Amazon.

Ewan Todd
ths is how it has to be i guess, i just wanted to know if there was hope. thanks anyway. p.s python is useless for "real world" stuff
spearfire
I don't think python is useless for real world stuff. Off the top of my head, I know Pixar uses it for a core piece of their operation.
Ewan Todd
A: 

About all the C that you really need to know is

  1. C types are much faster than Python types (adding to C ints or doubles can be done in a single clock cycle) but less safe (they are not arbitrarily sized and may silently overflow).
  2. C function (cdef) calls are much faster than Python (def) function calls (but are less flexible).

This will get you most of the way there. If you want to eek out that last 10-20% speedup for most applications, there's no getting around knowing C, and how modern processes work (pointers, cache, ...).

+2  A: 

Have you seen this: http://www.perrygeo.net/wordpress/?p=116 seems like a pretty good overview. You could also have a look at the source in pyzmq and gevent - they use Cython for their core code.

Ben

Ben Ford