Pyrex praise (repeat)

François Pinard pinard at iro.umontreal.ca
Tue Aug 6 18:23:41 EDT 2002


[Terry Hancock]

> I tried a pure-python implementation, but there didn't seem to be any
> way to do it without just looping through all the list members, and this
> took an awful long time to run.  (Too long for the game).

Going from Python to C should normally give you an increase of speed, but
roughly through a multiplication by some constant.  It is often better to
find a better lookup algorithm, you may find more impressive improvements
this way, maybe enough to stop considering going through C.

> the overhead of using something like C++ seems prohibitive [...]

Quite.  If you have the choice, code everything in Python, profile to find
out what needs to be sped up, and rewrite only a few parts in C or C++.
C is usually sufficient.

> Also, I need to do a fast loop through the Python list, I think, so I
> need it to integrate well with Python, and I don't really want to recode
> any basic Python types!

There is a price at going back and forth between Python and C.  You'll get
the better speed gain by changing side, and doing a significant amount of
computation on the C side before returning to the Python side.  This might
mean that you should initially copy your Python lists into C arrays, say,
once and for all if possible, before launching searches.

> to get a handle on the Python/C API [...] I have the feeling the result
> is going to be kind of clunky (from the development point of view --
> lots of lines of code).

And also, what might be difficult debugging.

> Should I chuck it, and learn Pyrex instead?  [...] Or should I just
> plow through my resistance on the Python/C API?  (Maybe the wall's not
> as thick as it looks?).

Surely with Pyrex, by comparison, the wall is rather thin.  If I were you,
I would become acquainted in Pyrex, enough to keep resisting to the Python/C
API, as far as resisting would still have a meaning or usage.  In fact,
this gives a whole new meaning to the "Resistance is futile!" statement. :-)

> What does either of them do to your build/distribution complexity?
> (I.e. can I use distutils with either -- I've never used it, but I plan
> to someday :-) ).

Both are usable with Distutils.  For one, I still use a mix of Makefiles
with `setup.py' files in my projects.  For Pyrex things, I choose to trigger
the work through `make' -- Distutils with Pyrex generates many compilation
warnings which are apparently innocuous, but which bother me: I prefer _not_
getting used at seeing diagnostics, and consequently, want clean compiles.

-- 
François Pinard   http://www.iro.umontreal.ca/~pinard




More information about the Python-list mailing list