What about an EXPLICIT naming scheme for built-ins?
Alex Martelli
aleaxit at yahoo.com
Mon Sep 6 04:15:26 EDT 2004
Peter Otten <__peter__ at web.de> wrote:
> Alex Martelli wrote:
>
> > $ python ~/cb/timeit.py -s'r=range(1000)' 'list(reversed(r))'
> > 10000 loops, best of 3: 99 usec per loop
> >
> > $ python ~/cb/timeit.py -s'r=range(1000)' '[x for x in reversed(r)]'
> > 1000 loops, best of 3: 706 usec per loop
> >
> > Dunno 'bout you guys, but I can't really afford to throw away a factor
> > of SEVEN in a perfectly ordinary task in such a fashion...
>
> For the sake of completeness, bypassing reversed() gains you another factor
> of two and might be worthwile, too, if you need the resulting list.
Very good point!
>
> $ 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
I didn't expect that big a gain for [::-1] but that just goes to show:
expectations ARE tricky when performance is concerned, measurements are
a good idea.
>
> For loops reversed() remains the solution of choice, even if memory
> consumption is not a problem:
>
> $ python2.4 timeit.py "r=range(1000)" "for i in reversed(r): pass"
> 10000 loops, best of 3: 87.2 usec per loop
> $ python2.4 timeit.py "r=range(1000)" "for i in r[::-1]: pass"
> 10000 loops, best of 3: 99.7 usec per loop
An important distinction indeed. Thanks!
Alex
More information about the Python-list
mailing list