4 ms·
CPython is built more naively for lots of operations. A global lookup is a dictionary lookup. Getting a symbol out of an object is a dictionary lookup. An objec
by koenigdavidmj 11y ago
CPython is built more naively for lots of operations. A global lookup is a dictionary lookup. Getting a symbol out of an object is a dictionary lookup. An object creation is always an allocation; variables don't go on the stack. Everything is reference counted. The old 'range' builtin would create an actual list of that length in memory, rather than just keep track of the start and end points like a C-style for loop.
Other implementations could theoretically do better. PyPy can detect after a while that a variable can safely be declared on the stack, and so it will start to do that. If the object type is known, then a field lookup becomes just adding an offset to the pointer. It can even use different implementations for the same Python-visible classes (to store a range result in constant space, but still appear to be the full list, or use a different type of dictionary for objects and {}, or other things like that).