Python performance notes...

Bernhard Herzog herzog at
Tue May 23 15:08:26 CEST 2000

Courageous <jkraska1 at> 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.
> 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:


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  |

More information about the Python-list mailing list