What about an EXPLICIT naming scheme for built-ins?

Peter Otten __peter__ at web.de
Mon Sep 6 03:23:00 EDT 2004


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. 

$ 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

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


Peter 




More information about the Python-list mailing list