Slow Python - what can be done?

Christian Tismer tismer at stackless.com
Tue Mar 23 00:39:11 CET 2004


Jason wrote:

> Hey,
> 
> I'm an experience programmer but new to Python. I'm doing a simple
> implementation of a field morphing techinique due to Beier and Neely
> (1992) and I have the simple case working in Python 2.3 - but it's
> REALLY slow.

That's ok.

> Basically, you specify two directed line segments in the coordinate
> system of a raster image and use the difference between those two
> lines to transform the image.
> 
> for a 400 x 600 image, python takes about 30 seconds to run the
> algorithm. This seems way to slow - I would expect it to run in a
> matter of a few seconds. Here's the code: what should I do to speed
> things up? I know I'm going to get a "do it in C/make a C extension"
> but that defeats the purpose: I'd like to know what Python can do
> here.

Well, I am on this least for years now, and I know most
of the answers in advance (and most have been here),
so I'm not adding new stuff how to optimize this.

Although in fact your inner point class raises the expenses
considerably.

If you need a fast algorithm right now, you need to change
your program, probably by writing or using C extensions.

This has always been the flip side of Python: When the real
fun begins, Python is too slow. It is great on everything
but speed. Advice then mostly concentrates on how to make
you using something else for the time critical code.

This is what we will solve by the PyPy project. We are besides
other things automating a way to analyse and optimize Python
programs for C-like speed. The advantage is that you can stick
with your Python program. It will just be very fast, compared
to, say, C. (We don't know yet  *how exactly* this will compare :-)

So, if you have the time, wait for two years. Or use Psyco, and
rewrite your Points to use simple Tuples, or use NumPy.
But please keep a copy of your initial approach.
I would be very interested to see how this compares when
we have the first results from our PyPy project.

cheers - chris

-- 
Christian Tismer             :^)   <mailto:tismer at stackless.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  mobile +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/





More information about the Python-list mailing list