[Pyrex] any ideas to speed this up?

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Feb 23 05:26:25 CET 2006


Brian Blais wrote:

> for e in 100:   # outer loop
>     for i in 1000:  # inner loop
>         (steps 1-3)

> I tried Pyrex, which 
> should work very fast on such a problem, takes about 8.5 seconds per 
> outer-loop step.

You should be aware that a Python-style for-loop
in Pyrex is not very efficient, since it goes through
the iterator protocol just like Python does.

There's another kind of for-loop in Pyrex that
looks like this:

   cdef int e
   for e in 0 <= e < 100:
     ...

That translates into straight C code, and is much
faster.

Also, as a general principle, make sure you declare
*all* variables used in time critical parts of the
code as having C types (as I did with e above).
Otherwise there will be lots of Python <-> C
conversions going on, which will slow you down
enormously.

You may also find it useful to take a look over the
C code generated by Pyrex and make sure there are
no Python-C API calls (PyXXX_YYY functions) in any
critical areas. If there are, it means there are
some Python variables being used somewhere.

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | Carpe post meridiam!          	  |
Christchurch, New Zealand	   | (I'm not a morning person.)          |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+



More information about the Pyrex mailing list