Do deep inheritance trees degrade efficiency?

Discussion in 'Python' started by Anthra Norell, Mar 18, 2009.

  1. 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 ())))).

    Frederic
     
    Anthra Norell, Mar 18, 2009
    #1
    1. Advertising

  2. Anthra Norell

    Peter Otten Guest

    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
     
    Peter Otten, Mar 19, 2009
    #2
    1. Advertising

  3. Anthra Norell

    Peter Otten Guest

    Peter Otten wrote:

    > 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.


    "currently" meaning 2.5 here...

    > $ 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


    [Christian Heimes]
    > Your assumption is no longer true. Starting with Python 2.6 and 3.0 the
    > lookup of attributes is cached. You can find detailed information by
    > searching for VERSION_TAG in the source code.


    I missed that change. Here are the 2.6 timings:

    $ python2.6 -m timeit -s"from inherit import a" "a.m"
    10000000 loops, best of 3: 0.171 usec per loop
    $ python2.6 -m timeit -s"from inherit import b" "b.m"
    10000000 loops, best of 3: 0.169 usec per loop

    Impressing.

    Peter
     
    Peter Otten, Mar 19, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Rebert
    Replies:
    3
    Views:
    406
    Mike Howard
    Mar 28, 2009
  2. jacob navia

    Binary search trees (AVL trees)

    jacob navia, Jan 3, 2010, in forum: C Programming
    Replies:
    34
    Views:
    1,491
    Dann Corbit
    Jan 8, 2010
  3. Guest
    Replies:
    0
    Views:
    161
    Guest
    Aug 7, 2004
  4. Uncutstone Wu
    Replies:
    9
    Views:
    176
    uncutstone
    Sep 24, 2006
  5. Kenneth Kalmer
    Replies:
    2
    Views:
    180
    Kenneth Kalmer
    Jul 24, 2007
Loading...

Share This Page