Re: modified reverse
- Posted by Igor Kachan <kinz at peterlink.ru> Aug 06, 2001
- 480 views
Ýòî ñîîáùåíèå â ôîðìàòå MIME ñîñòîèò èç íåñêîëüêèõ ÷àñòåé. ------=_NextPart_000_01C11ED1.E027FE00 Robert Craig writes: > Derek Parnell writes: > > Strange but I found rforno's version slightly slower (96%) than the > > standard. > > Yes, I searched the mailing list archives, and found a comparison > I did between the current reverse() and a version identical to > rforno's 3 years ago. I was using my old Pentium-150 machine, > and found the current reverse() was faster. That's why I chose it. > Now I have a Pentium II with a different cache size etc. I have some results under extremal conditions of swapping on Dos-32 platform. Timing on the complex sequence with length 80000 was : reverse () -- 52.9 92.93 -- RDS reverse1() -- 58.05 31.75 -- rforno reverse2() -- 124.07 165.49 reverse3() -- 107.93 102.49 reverse4() -- 107.93 180.87 reverse2() -- 53.94 66.19 -- Derec On Windows in Dos console on the simple sequence with length 194169 without swapping results were : RDS 2.75 2.80 2.97 2.96 2.75 r1 3.73 4.34 4.07 3.95 5.21 r2 3.73 3.84 3.84 3.79 3.85 r3 4.56 4.56 4.51 4.56 4.62 r4 4.51 4.61 4.5 4.5 4.56 Derec 2.86 2.96 3.02 2.91 3.08 But on plain Dos Derec's function seems the fastest, not always. I notice a problem -- with tick_rate(100), swapping on plain Dos immediately hungs up and swap-file gets boo-boo, scandisk helps. With tick_rate(300), machine hungs up. My machine is simplest -- 386 I have attached my test program, try and choose reverse which you like. Again Regards, Igor Kachan kinz at peterlink.ru ------=_NextPart_000_01C11ED1.E027FE00 Content-Type: application/octet-stream; name="Reverse.ex" Content-Transfer-Encoding: quoted-printable Content-Description: Reverse.ex (EX ) Content-Disposition: attachment; filename="Reverse.ex" --rforno --Rob: --The following modified reverse routine is simpler and, most of the = times, --slightly faster than the standard one. However, I noticed that the --comparison between the two is affected by the type of data (integer or = not, --simple or compound sequence), and even it seems that the way of = generating --the data (by means of a single sentence or by a loop) affects the = resulting --timings. Any explanation for such a behavior? sequence S S=3D{} sequence R R=3D{} sequence n n=3D{} atom T global function reverse(sequence s) -- reverse the top-level elements of a sequence. -- Thanks to Hawke' for helping to make this run faster. integer lower, n sequence t =20 n =3D length(s) t =3D repeat(0, n) lower =3D 1 for upper =3D n to floor(n/2)+1 by -1 do t[upper] =3D s[lower] t[lower] =3D s[upper] lower +=3D 1 end for return t end function global function reverse1(sequence s) integer n sequence t n =3D length(s) t =3D repeat(0,n) for i =3D 1 to n do t[n] =3D s[i] n -=3D 1 end for return t end function global function reverse2(sequence s) integer n sequence t t =3D s n =3D length(s) for i =3D 1 to n do t[n] =3D s[i] n -=3D 1 end for return t end function global function reverse3(sequence s, integer n) sequence t t =3D s for i =3D 1 to n do t[n] =3D s[i] n -=3D 1 end for return t end function global function reverse4(sequence s, integer n) R =3D s for i =3D 1 to n do R[n] =3D s[i] n -=3D 1 end for return R end function global function reverse5(sequence s) integer n,m n =3D length(s) R =3D s m =3D n for i =3D 1 to n do R[m] =3D s[i] m -=3D 1 end for return R end function global function reverse6(sequence s) integer lower, n, max sequence t =20 n =3D length(s) t =3D repeat(0, n) lower =3D 1 -- Force an integer result. max =3D floor(n/2)+1 =20 -- Now 'upper' knows that it can safely do integer comparisions -- because both 'n' and 'max' are integers. for upper =3D n to max by -1 do t[upper] =3D s[lower] t[lower] =3D s[upper] lower +=3D 1 end for return t end function procedure init(integer N) for i=3D1 to N do for j=3D1 to rand(50) do n &=3D rand(100000) end for S =3D append(S,n) S &=3D rand(100000) n=3D{} end for ? length(S) end procedure procedure test() T=3Dtime() R=3Dreverse(S) puts(1,"rds ")?time()-T R=3D{} T=3Dtime() R=3Dreverse1(S) puts(1,"r1 ")?time()-T R=3D{} T=3Dtime() R=3Dreverse2(S) puts(1,"r2 ")?time()-T R=3D{} T=3Dtime() R=3Dreverse3(S, length(S)) puts(1,"r3 ")?time()-T R=3D{} T=3Dtime() R=3Dreverse4(S, length(S)) puts(1,"r4 ")?time()-T R=3D{} T=3Dtime() R=3Dreverse5(S) puts(1,"r5 ")?time()-T R=3D{} T=3Dtime() R=3Dreverse6(S) puts(1,"r6 ")?time()-T R=3D{} end procedure init(18000) test() ------=_NextPart_000_01C11ED1.E027FE00--