Directly converting Python to C using the C API

Chris Rebert clp2 at rebertia.com
Thu Dec 3 21:19:07 EST 2009


On Thu, Dec 3, 2009 at 6:09 PM, Emmanuel Bengio <bengioe at gmail.com> 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?

You're going to have to check that xrange() hasn't been rebound for starters.

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list