1. reverse() function, was:Re: Win32Lib Update
- Posted by Ad Rienks <Ad_Rienks at COMPUSERVE.COM> Nov 28, 1998
- 351 views
Ralf wrote: >Why not start a thread ? OK, if you insist >function reverse_flat (sequence s) >-- Reverses only the top level >sequence result > result =3D {} > for index =3D 1 to length(s) do > result =3D prepend(result, s[index]) > end for > return result >end function >-- The above is the most clean and fastest, as far as I can tell. But I thought that appending an prepending is slower than first creating = a sequence and then filling it. Or isn't that true for small sequences? Som= e please benchmark above version against mine: function reverse_flat2(sequence s) integer len sequence temp len =3D length(s) temp =3D repeat("", len) for n =3D 1 to len do temp[n] =3D s[len - n + 1] end for return temp end function -- TIA, Ad Rienks >function reverse (object x) >-- Reverses all levels in the sequence >sequence result > if atom (x) then > return x > end if > result =3D {} > for index =3D 1 to lenght(s) do > result =3D prepend (result, reverse (s[index])) > end for >return result >end function >-- This too is the fastest (and cleanest possible). I know I can avoid o= ne >level of recursion, however Euphoria's >for-statement-block-containing-only-one-statement-optimization will be >missed. And for larger sequences (those who need the extra speed the mos= t) >this will make a significant difference. >However, the above is all speculative, until some one starts benchmarkin= g on >this. >Ralf
2. Re: reverse() function, was:Re: Win32Lib Update
- Posted by Irv Mullins <irv at ELLIJAY.COM> Nov 28, 1998
- 343 views
-- Here are your (Ralf & Hawke's) versions, plus a couple of other -- ways off the top of my head, timed: sequence before, after, start, fini before = "Now is the time for all good men to come get drunk at the party." function Reverse1 (sequence s) -- 7 seconds for 100,000 iterations sequence r r = {} for i = 1 to length(s) do r = prepend(r,s[i]) end for return r end function function Reverse2(sequence data) -- 4 seconds for 100,000 its. sequence result integer len, from len = length(data) from = len --i really could reuse len.... result = repeat(0,len) --BOOM, call alloc *once* for toindex = 1 to len do result[toindex] = data[from] from = from - 1 --and this takes 1 single tick... end for return result end function function Reverse3 (sequence s) -- 6 seconds for 100,000 its. atom temp, left, right left = 1 right = length(s) while left < right do temp = s[left] s[left] = s[right] s[right] = temp left = left + 1 right = right - 1 end while return s end function function Reverse4 (sequence s) -- 5 seconds atom x sequence r x = length(s) r = repeat(0,x) for i = 1 to x do r[i] = s[x-i+1] end for return r end function start = date() for i = 1 to 100000 do after = Reverse4(before) -- change the nuber to test other functions end for fini = date() printf(1,"Start: %02d:%02d:%02d\n",{start[4],start[5],start[6]}) printf(1,"Fini: %02d:%02d:%02d\n",{fini[4],fini[5],fini[6]}) puts(1,after) -- I feel sure there is a faster way. Any takers? -- Irv
3. Re: reverse() function, was:Re: Win32Lib Update
- Posted by Irv Mullins <irv at ELLIJAY.COM> Nov 28, 1998
- 373 views
Sorry: I didn't mean to leave Ad out of the list :) Irv