*-unpacking

Alex Martelli aleax at aleax.it
Fri Oct 3 18:28:04 EDT 2003


David Mertz wrote:

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

Right, and that's what we were discussing.


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

Nope.  We're discussing situations where ONE pop from the start is
needed; and you claimed I knew that in that case reversing the list
and popping from the end was faster.

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

It's exactly the right behavior and exactly the relevant thing.  Note
the [:] which I discussed in my post -- I'm not interested in popping
all items one after the other (which would have absolutely nothing to do
with 'car, *cdr = thelist'), I'm interested in popping ONE item -0- the
first one.  So, the comparison is between one pop(0) and one
reverse() then one pop() [of course, not with a [:] -- except for
the need to cooperate with timeit.py's measurements in what would
otherwise be a 'destructive' operation and thus unmeasurable by
it].  (My error, as Michael helped me see, was in not using -c or
making my machine perfectly quiescent -- I _thought_ it was but a
little experimentation soon showed it was anything but; anyway,
even _with_ better measurement, reverse-then-pop does turn out
to be faster -- by a miniscule margin, on this release and on this box --
than pop(0) -- I'm not quite sure I "know" it, yet, though...).


Alex





More information about the Python-list mailing list