Jp Calderone wrote:
On Fri, 17 Dec 2004 13:24:43 -0600, Mike Meyer <[EMAIL PROTECTED]> wrote:
Jeff Shannon <[EMAIL PROTECTED]> 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:
[EMAIL PROTECTED]:~$ timeit -s "def foo(): pass" "foo()"
1000000 loops, best of 3: 0.382 usec per loop
[EMAIL PROTECTED]:~$ timeit -s "class Foo:
def foo(self): pass
f = Foo()" "f.foo()"
1000000 loops, best of 3: 0.611 usec per loop
[EMAIL PROTECTED]:~$
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:
[EMAIL PROTECTED] ~ $ timeit -s "def foo(): pass" "foo()" 1000000 loops, best of 3: 0.963 usec per loop
[EMAIL PROTECTED] ~ $ timeit -s "class Foo: def foo(self): pass f = Foo()" "f.foo()" 1000000 loops, best of 3: 1.71 usec per loop
[EMAIL PROTECTED] ~ $ 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.
[EMAIL PROTECTED] ~ $ 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 -- http://mail.python.org/mailman/listinfo/python-list