On Mon, Dec 01, 2003 at 04:41:30PM +0100, Klaus Bosau wrote:

> On Mon, 1 Dec 2003, Benji Fisher wrote:

>

> > [...]

> >

> > I assume that this worked when the InvertString() function was

> > first posted to the list, but broken by a later patch. The oldest

> > version of vim that I have lying around is 6.1.400. Does anyone with

> > an older version get different results?

>

> (I recently upgraded to 6.2, so I can't answer your question.) Just for

> those who are interested.. Preben was indeed right, InvertString()

> turned out to be about twice as fast as a comparable solution based on a

> :while loop.

>

> For

>

> function! InvertString(str)

> let inverted = ''

> let n = strlen(a:str) - 1

> while n >= 0

> let inverted = inverted . a:str[n]

> let n = n - 1

> endwhile

> return inverted

> endfunction

>

> and

>

> let n = 3000

> while n > 0

> let x = InvertString('abcdefghijklmnopqrstuvwxyz')

> let n = n - 1

> endwhile

>

> I got 10 and 5 seconds..

>

> Klaus

I get similar results: 5 seconds for the original version, 9

seconds for yours. This version takes 6 seconds:

function! IS2(str)

let n = strlen(a:str)

let m = n/2

let inverted = (n%2 ? a:str[m] : '')

while m

let inverted = a:str[n-m] . inverted . a:str[m-1]

let m = m-1

endwhile

return inverted

endfunction

It would probably be faster if someone got to this item from todo.txt:

8 Add ":let var[{expr}] = {expr}". When past the end of "var" just ignore.

The recursive version takes 14 seconds.

--Benji Fisher