Python performance notes...

Christian Tismer tismer at tismer.com
Tue May 23 07:19:53 EDT 2000


Courageous wrote:
> 
> I did a simple for loop and tested it, trying a variety of
> length loops. It took a bit of time to get this right, as
> for smaller loops, the test was biased by the relative high
> cost of function invocation. When it settled out, a for-loop
> in python is about 100 times slower in python than the
> equivalent in ANSI C. Irrespective of function invocation
> overhead, the decision to go native pays off almost
> immediately, with time differences in python versus native
> code being noticeable with as low as 100,000 simple iterations.
> 
> Writing native methods in python is, fortunately, quite easy.

And creating bugs, as well. :-)

> TO WIT:
> 
> PyObject* Test ( PyObject* self, PyObject* args )
> {
>     int i;
>     for(i=0;i<100000;i++);
>     return Py_None;
> }

You didn't INCREF your result.
Call this function often enough, and your system will crash,
since Py_None has an empty deallocator.

Furthermore, I'd suggest to do a different test than the above.
Empty loops don't happen in reality, and it makes limited
sense to compare a Python loop against a compiler, which in
this case even has the chance to optimize the full thing
away.
Instead, I would at least do some action in the loop that
involves some Python objects. In order to figure the
real loop cost out of it, I would run two different tests:
One with one action per loop cycle and on with two.
This should give a good estimate of the cost of the action,
and it can than be factored away to find the loop cost.

I'd also like to have more info on what computation
your conclusion is based. If you only have a loop
that does a trivial task, the loop overhead might
count. But if there are actions which take some time,
then I doubt that just removing the loop would buy
me so much.
The calling overhead of the objects in your code is still
there, and unless you leave Python totally and use native
C structures all the time, you will not gain much more
than 30, maybe 40 percent of speed by avoiding the interpreter.

This is IMHO not enough to justify writing C code by hand,
with the additional risk of poking holes into a quite
stable system. Using some automation like P2C for this
*might* make sense, while the speedup is still not convincing
enough to use a compiler.

ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com




More information about the Python-list mailing list