4 ms·
Didn't know the timeit module. However, the presented usage to time list reversing is ill: The timing is dominated by the list creation, which should go into se
by lysium 15y ago
Didn't know the timeit module. However, the presented usage to time list reversing is ill: The timing is dominated by the list creation, which should go into setup. Then I'll get:
$ python -mtimeit -s'l=range(1000)' 'l[::-1]'
100000 loops, best of 3: 3.05 usec per loop
$ python -mtimeit -s'l=range(1000)' 'l.reverse()'
1000000 loops, best of 3: 0.535 usec per loop
$ python -mtimeit -s'l=range(1000)' 'a=reversed(l)'
1000000 loops, best of 3: 0.39 usec per loop
So, instead of reversing the list in place, creating a new, reversed list seems to be faster.
- Sharlin 15y ago>>> reversed([]) <listreverseiterator object at 0xb73ff7cc> reversed() does not create a new list, it just returns a reverse-order view of the same list. Of course, often this is exactly what is needed.