Python performance notes...

Bernhard Herzog herzog at online.de
Tue May 23 09:08:26 EDT 2000


Courageous <jkraska1 at san.rr.com> writes:

> 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.
> TO WIT:
> 
> PyObject* Test ( PyObject* self, PyObject* args )
> {
>     int i;
>     for(i=0;i<100000;i++);

Are you sure that your C-compiler didn't just optimize this loop away?

And you should add an INCREF here:

      Py_INCREF(Py_None);

It may work as it is for a while because there are lots of other
references to None around, but calling this function a few hundred times
will produce a segfault.

>     return Py_None;
> }
> 

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list