Directly converting Python to C using the C API

Emmanuel Bengio bengioe at gmail.com
Thu Dec 3 21:09:58 EST 2009


I'm fairly new to the python world, and I often heard critics about it's
speed, and also about it's "non-compilability".
Then I thought, there is a complete C API that manages Python itself.
Would it be feasible to transform pure Python code into pure Python "C API"
code? It would spare the parsing time, which seems to be the an important
factor in Python's speed.


Given the following example:
x=0
for i in xrange(100000):
    x+=x*i

On my machine, this takes 79ms to run.

while this:
PyObject* x = PyInt_FromLong(0);
int i;
for (i=0;i<100000;i++)
{
    x=PyNumber_Add(x,PyNumber_Multiply(x,PyInt_FromLong(i)));
}
takes about 16 ms to run.

Any thoughts?

-- 


        Emmanuel Bengio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091203/bb9ec8da/attachment.html>


More information about the Python-list mailing list