to pass self or not to pass self
tjreedy at udel.edu
Wed Mar 17 20:55:35 CET 2010
On 3/17/2010 1:35 AM, Patrick Maupin wrote:
>>>> def a(s, count, lenfunc):
> ... for i in xrange(count):
> ... z = lenfunc(s)
>>>> >>> a('abcdef', 100000000, len)
>>>> >>> a('abcdef', 100000000, str.__len__)
> Running cPython 2.6 on my machine, len() runs about 3 times faster
> than str.__len__(). The overhead of checking that an object is usable
> with a particular class method far outweighs the cost of creating the
> bound method!
Wow, this so surprised me, that I had to try it with 3.1 (on winxp), and
got a similar result (about 2.6x longer with str.__len__). This is a
real lesson in measure, don't guess, and how premature 'optimization'
may be a pessimization. Thanks.
Terry Jan Reedy
More information about the Python-list