On 15 Feb 2008, at 17:37, Guido van Rossum wrote:
> On Thu, Feb 14, 2008 at 4:52 PM, Raymond Hettinger <[EMAIL PROTECTED]>
> wrote:
>> Would it be worthwhile to leave reversed(s) and s[::-1] as the two
>> obvious ways to do it (ways that work with any Sequence, not just
>> lists)?
>
> I think we should keep it. There's no faster way to reverse a list
> in place.
Out of curiosity, I checked just how much faster it really is. The
answer is that for in place reversal it's more than an order of
magnitude faster than using either of the suggested alternatives:
>>> timeit.Timer('s.reverse()', setup='s=range(1000)').timeit()
0.80964398384094238
>>> timeit.Timer('s[:] = s[::-1]', setup='s=range(1000)').timeit()
9.2884359359741211
>>> timeit.Timer('s[:] = reversed(s)',
setup='s=range(1000)').timeit()
16.757926940917969
Not surprising, given that no objects were created or harmed in the
reversal of this list...
Interestingly, it's not the fastest way to make a reverse copy:
>>> timeit.Timer('s2 = list(s); s2.reverse()',
setup='s=range(1000)').timeit()
6.4023230075836182
>>> timeit.Timer('s2 = s[::-1]', setup='s=range(1000)').timeit()
5.3177969455718994
>>> timeit.Timer('s2 = list(reversed(s))',
setup='s=range(1000)').timeit()
12.906921148300171
(Times take on an Intel Mac, Python 2.5.1)
Nicko
_______________________________________________
Python-3000 mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-3000
Unsubscribe:
http://mail.python.org/mailman/options/python-3000/archive%40mail-archive.com