in-place string reversal

Sion Arrowsmith siona at chiark.greenend.org.uk
Tue Mar 28 18:32:29 CEST 2006


Felipe Almeida Lessa  <felipe.lessa at gmail.com> wrote:
>Em Ter, 2006-03-28 às 16:03 +0100, Sion Arrowsmith escreveu:
>> >>> "".join(reversed("foo"))
>$ python2.4 -mtimeit '"".join(reversed("foo"))'
>100000 loops, best of 3: 2.58 usec per loop

But note that a significant chunk is the join():

$ python2.4 -mtimeit '"".join(reversed("foo"))'
100000 loops, best of 3: 2.72 usec per loop
$ python2.4 -mtimeit 'reversed("foo")'
1000000 loops, best of 3: 1.69 usec per loop

>$ python2.4 -mtimeit '"foo"[::-1]'
>1000000 loops, best of 3: 0.516 usec per loop

Yeah, I forget about [::-1] due to the high profile of the introduction
of reversed(). Well, of sorted(), and reversed() coming along for the
ride. And at some point reversed() will become a win:

$ python2.4 -mtimeit 'reversed(range(200))'
100000 loops, best of 3: 6.65 usec per loop
$ python2.4 -mtimeit 'range(200)[::-1]'
100000 loops, best of 3: 6.88 usec per loop

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list