Interesting performance about attribute lookup
Mark Hammond
mhammond at skippinet.com.au
Sun Oct 26 22:52:23 GMT 2008
> so it seems that looking up the "copy.append" attribute 100,000 times
> takes 7.4 msec (15.6-8.22), while actually doing the append only takes
> 5.46 msec (8.22-2.76).
It does seem counter-intuitive, but it does make sense. In both the 'call method' and 'attribute lookup' cases , Python needs to make the same basic 'internal call' to perform the operation - into the C implemented append method, or the C implemented tp_getattro() slot - so both suffer the same 'setup' overhead before they can do anything.
Of the operations themselves, list.append() is extremely cheap - list objects over allocate proportional to the list size, so appending an item is usually only a pointer copy and a reference bump. OTOH, the attribute lookup winds through PyObject_GenericGetAttr(), which checks the type of the object and performs a dictionary lookup of the name. I'd expect the performance to get even worse if you used a subclass of a list object (best I can tell, a dict lookup per base class will be done until it is found).
It almost sounds like a job for the byte-code optimizer, but Python's nature means that is almost impossible to always catch that while always staying correct...
IIUC-ly,
Mark
More information about the bazaar
mailing list