Python performance notes...

Courageous jkraska1 at san.rr.com
Tue May 23 03:07:41 EDT 2000


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++);
    return Py_None;
}

static PyMethodDef embedxmethods[]=
{
    {"Test",             Test,            METH_VARARGS},
    {NULL, NULL }
};

void _declspec(dllexport) initembedx ()
{
    Py_InitModule("embedx", embedxmethods);
}

When you realize that the last two groups are basically cookie
cutter, this becomes not bad at all.



C/



More information about the Python-list mailing list