What about an EXPLICIT naming scheme for built-ins?
carribeiro at gmail.com
Mon Sep 6 15:29:02 CEST 2004
On Mon, 06 Sep 2004 09:23:00 +0200, Peter Otten <__peter__ at web.de> wrote:
> For the sake of completeness, bypassing reversed() gains you another factor
> of two and might be worthwile, too, if you need the resulting list.
> $ python2.4 timeit.py -s"r = range(1000)" "[i for i in reversed(r)]"
> 10000 loops, best of 3: 148 usec per loop
> $ python2.4 timeit.py -s"r = range(1000)" "list(reversed(r))"
> 100000 loops, best of 3: 19.5 usec per loop
> $ python2.4 timeit.py -s"r = range(1000)" "r[::-1]"
> 100000 loops, best of 3: 9.17 usec per loop
So we now have three different idioms for what could potentially be a
single one -- a call to the reversed() builtin. Isn't it a good enough
reason to think about it?
BTW a solution just crossed my mind while writing now. I'm just
brainstorming, so please be gentle :-) It goes like this:
a) reversed_list = reversed(mylist).list (using a read-only property)
b) reversed_list = reversed(mylist).list() (using a method)
The idiom above could be implemented to run just as fast as the
slicing version (r[::-1]) (in fact, it could just return the slice
with very little setup overhead). It's not ideal, but may be a
compromise between the current scenario (reversed() returning an
iterator) and the lack of obvious choices (as shown by the existence
of widely different idioms).
Consultoria em Projetos
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com
More information about the Python-list