Pyrex list/array

John Machin sjmachin at lexicon.net
Sat Jun 3 21:39:55 EDT 2006


On 4/06/2006 4:56 AM, Jim Lewis wrote:
> I'm trying to move a function into pyrex for speed.

You probably didn't expect the Inquisition; nobody does. But here it is, 
nice red uniforms and all:

1. What is your speed requirement and how far short of that are you at 
the moment?
2. Are you sure there is no Python or third-party module that does what 
you want?
3. Is your algorithm the best possible?
4. Is your Python implementation of that algorithm the best possible? 
Have you exposed it to the critical gaze of the speed-freaks in this 
newsgroup?
5. Does your architecture support psyco? If so, have you tried that and 
what were the results?

> The python side
> needs to pass a list to the pyrex function. Do I need to convert to
> array or something so pyrex can generate tight code? I'm not clear how
> to do this.
> 

The question might be better asked on the Pyrex mailing list.

You don't need to convert a list to a C array, and it may not even be 
possible, depending on what type(s) of data you have in the list.

Almost any Python code is also valid Pyrex code. For a start, just 
compile your function with Pyrex and compare the speed. What you do next 
is going to depend very much on what operations you are performing on 
the list and the objects it contains. Watch out for Python built-ins 
like range, xrange, ord, chr, abs, bool, int(a_number), float(a_number), 
divmod, max/min (two_numeric_args). In almost all cases you get cheap 
wins by replacing use of these by simple C-like code -- provided of 
course you are absolutely sure you know what types you are dealing with.

HTH,
John



More information about the Python-list mailing list