__delitem__ affecting performance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Karl H.

    #1

    __delitem__ affecting performance

    Hi,

    I was performing some timing tests on a class that inherits from the
    built-in list, and got some curious results:

    import timeit

    class MyList(list):
    def __init__(self):
    list.__init__(s elf)
    self[:] = [0,0,0]

    def __delitem__(sel f,index):
    print 'deleting'

    ml = MyList()

    def test():
    global ml
    ml[0] += 0
    ml[1] += 0
    ml[2] += 0

    t = timeit.Timer("t est()","from __main__ import test")
    print t.timeit()
    >4.1165138267 6
    import timeit

    class MyList(list):
    def __init__(self):
    list.__init__(s elf)
    self[:] = [0,0,0]

    ml = MyList()

    def test():
    global ml
    ml[0] += 0
    ml[1] += 0
    ml[2] += 0

    t = timeit.Timer("t est()","from __main__ import test")
    print t.timeit()
    >2.2326859138 3
    Does anybody know why defining __delitem__ is causing the code to run
    slower? It is not being called, so I don't see why it would affect
    performance. Overriding other sequence operators like __delslice__ does
    not exhibit this behavior.

    The speed difference doesn't really bother me, but I am curious.

    I used Python 2.4 for this test.

    -Karl
  • Fredrik Lundh

    #2
    Re: __delitem__ affecting performance

    Karl H. wrote:
    Does anybody know why defining __delitem__ is causing the code to run
    slower? It is not being called, so I don't see why it would affect
    performance.
    probably because overriding portions of the internal sequence slot API
    (tp_as_sequence ) means that Python needs to do full dispatch for all
    members of that API, instead of keeping things at the C level.

    </F>

    Comment

    Working...