*-unpacking

David Mertz mertz at gnosis.cx
Fri Oct 3 13:23:17 EDT 2003


Alex Martelli <aleax at aleax.it> wrote previously:
|I'm not sure what this 'more' is about, actually.  Greg's case is
|currently best solved [IMHO] with
|    args = cmdstring.split()
|    command = args.pop(0)

Part of his description was "I might or might not have more in the
list."  My 'more' stuff was just illustrating handling that extra stuff.
Admittedly, that's not directly part of what 'car,*cdr=lst' solves.

|Actually, I don't -- both args.reverse() and args.pop(0) are
|O(len(args))

Hmmm...  I am pretty sure I have found Alex in an outright mistake.  A
shocking thing, if so :-).

|[alex at lancelot python2.3]$ python timeit.py -s'ags=range(1000)'
|'x=ags[:].pop(0)'
|10000 loops, best of 3: 24.5 usec per loop
|[alex at lancelot python2.3]$ python timeit.py -s'ags=range(1000)'
|'ags.reverse(); x=ags[:].pop()'
|10000 loops, best of 3: 23.2 usec per loop

The second operation does a '.reverse()' for every '.pop()', which is
both the wrong behavior, and not the relevant thing to time.

  >>> from time import clock
  >>> def reverse_then_pop(l):
  ...     start = clock()
  ...     l.reverse()
  ...     while 1:
  ...         try: l.pop()
  ...         except: break
  ...     print "%.2f seconds" % (clock()-start)
  ...
  >>> def pop_from_left(l):
  ...     start = clock()
  ...     while 1:
  ...         try: l.pop(0)
  ...         except: break
  ...     print "%.2f seconds" % (clock()-start)
  ...
  >>> reverse_then_pop(range(10000))
  0.03 seconds
  >>> pop_from_left(range(10000))
  0.39 seconds
  >>> reverse_then_pop(range(20000))
  0.06 seconds
  >>> pop_from_left(range(20000))
  1.47 seconds
  >>> reverse_then_pop(range(50000))
  0.14 seconds
  >>> pop_from_left(range(50000))
  11.73 seconds
  >>> reverse_then_pop(range(100000))
  0.29 seconds
  >>> pop_from_left(range(100000))
  140.77 seconds
  >>> reverse_then_pop(range(500000))
  1.41 seconds
  >>> pop_from_left(range(500000))
  ...abandoned after a few hours :-)...

Yours, David...

--
Keeping medicines from the bloodstreams of the sick; food from the bellies
of the hungry; books from the hands of the uneducated; technology from the
underdeveloped; and putting advocates of freedom in prisons.  Intellectual
property is to the 21st century what the slave trade was to the 16th.





More information about the Python-list mailing list