Re: Replacing characters (Matt: bug)
- Posted by Dan Moyer <DANIELMOYER at prodigy.net> Sep 17, 2002
- 475 views
This is a multi-part message in MIME format. ------=_NextPart_000_0033_01C25E8B.1A58FDA0 charset="iso-8859-1" The time results in my test for replacing one char with one are: Henri 3.95 Andy 2.8 Derek 3.3 Matt1 25.98 Matt2 29.93 zip of test attached. Dan Moyer ----- Original Message ----- From: "Derek Parnell" <ddparnell at bigpond.com> To: "EUforum" <EUforum at topica.com> Sent: Tuesday, September 17, 2002 5:31 AM Subject: Re: Replacing characters (Matt: bug) > > Also Matt, the line .... > > text = text[1..ix-1] & b & text[ix+lena..lent] > > can sometimes try and use negative slice length. > > And my effort doesn't work if you are shrinking the text. > > To fix my code, change ... > > t = repeat(0, (floor(length(s) / length(a)) + 1) * length(b)) > > to ... > > m = (floor(length(s) / length(a)) + 1) * length(b) > if m < length(s) then > m = length(s) > end if > t = repeat(0, m) > > Also mine fails if the searched-for object is zero length. > So change ... > if atom(a) then > a = {a} > end if > > to > > if atom(a) then > a = {a} > elsif length(a) = 0 then > return s > end if > > ---------------- > cheers, > Derek Parnell > ----- Original Message ----- > From: "Dan Moyer" <DANIELMOYER at prodigy.net> > To: "EUforum" <EUforum at topica.com> > Sent: Tuesday, September 17, 2002 5:28 PM > Subject: Re: Replacing characters (Matt: bug) > > > > Some tests (not exhaustive) of the 3 "replace characters" routines > recently > > shown here: > > > > Matt's now doesn't fail on replace last character, but doesn't actually > > replace the last character, & is *very* much slower than either of the > other > > two > > (one test: Henri: 3.9, Andy: 2.75 , Matt: 24.06) ; > > > > Henri/Mike "vulcan" routine is faster than Matt's but won't replace one > char > > with 2; > > > > Andy's is fastest. How much faster seems to vary. On a "long" sequence to > > peruse, it seemed twice as fast as Henri/Mike "vulcan", but on many > > repetitions of replacing in smaller sequence, it seems only 1.4 times > faster > > (see test results above). > > > > I've attached the tests I ran in case I did something dumb in them. > > > > Dan Moyer > > > > > > ----- Original Message ----- > > From: "Andy Serpa" <renegade at earthling.net> > > To: "EUforum" <EUforum at topica.com> > > Sent: Monday, September 16, 2002 3:56 PM > > Subject: RE: Replacing characters (Matt: bug) > > > > > > > Andy Serpa wrote: > > > > > > > > Henri Goffin wrote: > > > > >> global function replace_in_string(sequence s, object o, object n) > > > > > > > > > > > > > The "so" sequence is not necessary. You can compare the sequence > > > > directly to the atom "o": > > > > > > > > s += (s = o) * (n - o) > > > > > > > > > > > Of course this, while devoid of cleverness, is at least 10x faster (and > > > does swaps too): > > > > > > function replace_in_string(sequence s, object o, object n) > > > integer ni > > > > > > if atom(o) and atom(n) then > > > for i = 1 to length(s) do > > > if s[i] = o then > > > s[i] = n > > > end if > > > end for > > > return s > > > > > > elsif sequence(o) and sequence(n) and length(o) = length(n) then > > > > > > for i = 1 to length(s) do > > > ni = find(s[i],o) > > > if ni then > > > s[i] = n[ni] > > > end if > > > end for > > > return s > > > else > > > return -1 > > > end if > > > > > > end function > > > > > > > > > ------=_NextPart_000_0033_01C25E8B.1A58FDA0 Content-Type: application/x-zip-compressed; name="TstRepl2.zip"