View Single Post
  #2 (permalink)  
Old 03-19-2009, 08:14 AM
Peter Otten
Guest
 
Posts: n/a
Default Re: Do deep inheritance trees degrade efficiency?

Anthra Norell wrote:

> Would anyone who knows the inner workings volunteer to clarify whether
> or not every additional derivation of a class hierarchy adds an
> indirection to the base class's method calls and attribute read-writes.
> In C++, I suppose, a three-level inheritance would resolve into
> something like *(*(*(*(base_class_method ())))).


I think in C++ the compiler can often resolve the correct class statically.
Python currently walks through the entire hierarchy.

$ cat inherit.py
class A(object):
def m(self):
return 42


B = A
for i in range(1000):
class B(B): pass

a = A()
b = B()

if __name__ == "__main__":
print a.m()
print b.m()

$ python -m timeit -s"from inherit import a" "a.m"
10000000 loops, best of 3: 0.173 usec per loop
$ python -m timeit -s"from inherit import b" "b.m"
10000 loops, best of 3: 68.7 usec per loop

Peter
Reply With Quote