Why no list heritable type?

Steve Holden steve at holdenweb.com
Sat Dec 18 01:40:17 EST 2004


Jp Calderone wrote:

> 
> On Fri, 17 Dec 2004 13:24:43 -0600, Mike Meyer <mwm at mired.org> wrote:
> 
>>Jeff Shannon <jeff at ccvcorp.com> writes:
>>
>>
>>>Sion Arrowsmith wrote:
>>>Additionally, as I understand it UserList and UserDict are implemented
>>>entirely in Python, which means that there can be significant
>>>performance differences as well.
>>
>>Actually, UserList and UserDict are just wrappers around the builtin
>>types. So the performance hit is one Python function call - pretty
>>much neglible. But new code should still subclass the builtins.
> 
> 
>   Only tangentially related, but I'll bring it up anyway: method calls 
> are more expensive than function calls:
> 
>     exarkun at boson:~$ timeit -s "def foo(): pass" "foo()"
>     1000000 loops, best of 3: 0.382 usec per loop
>     exarkun at boson:~$ timeit -s "class Foo:
>         def foo(self): pass
>     f = Foo()" "f.foo()"
>     1000000 loops, best of 3: 0.611 usec per loop
>     exarkun at boson:~$ 
> 
>   This is due to the attribute lookup as well as the creation and 
> destruction of a bound method object for each call.
> 
>   Jp

Sure, but we can factor out the attribute lookup to see that it actually 
accounts for the majority of the slowdown:

sholden at dellboy ~
$ timeit -s "def foo(): pass" "foo()"
1000000 loops, best of 3: 0.963 usec per loop

sholden at dellboy ~
$ timeit -s "class Foo:
   def foo(self): pass
f = Foo()" "f.foo()"
1000000 loops, best of 3: 1.71 usec per loop

sholden at dellboy ~
$ timeit -s "class Foo:
   def foo(self): pass
f = Foo()
m = f.foo" "m()"
1000000 loops, best of 3: 1.09 usec per loop

Hmm, this laptop's getting a bit long in the tooth ... but method 
execution only seems to be about 20% slower than function execution. Not 
sure how long the "pass" takes. Hmmm.

sholden at dellboy ~
$ timeit pass
10000000 loops, best of 3: 0.163 usec per loop

So we seem to be looking (on my machine) at 0.8 usec per function call 
vs. 0.93 usec per method call.

regards
  Steve
-- 
Steve Holden               http://www.holdenweb.com/
Python Web Programming  http://pydish.holdenweb.com/
Holden Web LLC      +1 703 861 4237  +1 800 494 3119



More information about the Python-list mailing list