Reply to post: Re: Making Python faster

Faster Python: Mark Shannon, author of newly endorsed plan, speaks to The Register

Flocke Kroes Silver badge

Re: Making Python faster

In Python, every variable is an object which is mostly harmless for big complicated objects but really hurts for simple things like integers. Imagine a function has come up with the result 12345. C can put that in a register and return to the caller (and will do something stupid if the value being returned is too big to fit in the register). Python allocates some memory, stores a reference count of 1 at the start of that memory followed by a pointer to class int, then some number that represents the amount of memory needed to store the value of the int (python handles really big integers without making the programmer think hard) followed by the value 12345 and finally returns a pointer to the allocated memory. This process is so painful that python is already optimised by having instance for small integers already prepared at known locations so small integers can be returned by incrementing the reference count of the right one and returning a pointer to it.

From here things go further down hill. C is a typed language so the caller knows that an integer was returned. It can copy this integer wherever it is required, use it in arithmetic or simply forget about it. In python the caller only knows that some subclass of Object was returned. Something "simple" like a+b gets expanded to type(a).__add__(b). Luckily __add__ is optimised and does not require a dictionary look up. int.__add__ has to check the type of b and if int.__add__ does not understand it the Python interpreter tries type(b).__radd__(a) instead. Simple copying is not that bad: add one to the reference count and store a pointer where required but forgetting about an object is not trivial. The interpreter subtracts one from the reference count and if the result is zero calls type(a).__del__ then deallocates the memory assigned to the object.

Everything being a subclass of Object (almost always) makes creating new software in Python easier than C - at the cost of making the CPU do a huge amount work.

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon