Timing Difference: insert vs. append & reverse

Terry Reedy tjreedy at udel.edu
Mon Aug 2 21:03:30 EDT 2004


"John Keeling" <johnfkeeling at yahoo.com> wrote in message
news:35b736b9.0408021152.4c1ab752 at posting.google.com...
> expect. I've also noted an approximate two time difference between
> "del list[index] and list.pop(index)".

Here is one way to get some insight into such things:

>>> def ldel(lisp, index):
...   del lisp[index]
...
>>> def lpop(lisp, index):
...    return lisp.pop(index)
...
>>> import dis
>>> dis.dis(ldel)
          0 SET_LINENO               1

          3 SET_LINENO               2
          6 LOAD_FAST                0 (lisp)
          9 LOAD_FAST                1 (index)
         12 DELETE_SUBSCR
         13 LOAD_CONST               0 (None)
         16 RETURN_VALUE
>>> dis.dis(lpop)
          0 SET_LINENO               1

          3 SET_LINENO               2
          6 LOAD_FAST                0 (lisp)
          9 LOAD_ATTR                1 (pop)
         12 LOAD_FAST                1 (index)
         15 CALL_FUNCTION            1
         18 RETURN_VALUE
         19 LOAD_CONST               0 (None)
         22 RETURN_VALUE

You can be pretty sure that the specific opcode DELETE_SUBSCR (ipt) is
faster than the generic CALL_FUNCTION, which will eventually call (I
presume) the same C code.  (The extra attribute lookup and load take some
extra time too, but the call should be the main culprit).

Terry J. Reedy







More information about the Python-list mailing list