Directly converting Python to C using the C API

Philip Semanchuk philip at semanchuk.com
Thu Dec 3 21:21:02 EST 2009


On Dec 3, 2009, at 9:09 PM, Emmanuel Bengio wrote:

> 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?


This is such a good idea that it has become a popular project:
http://www.cython.org/







More information about the Python-list mailing list