Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!TARDIS.CRAY.COM!cargo From: cargo@TARDIS.CRAY.COM (David S. Cargo) Newsgroups: comp.lang.icon Subject: deleting characters Message-ID: <9001192303.AA01299@zk.cray.com> Date: 19 Jan 90 23:03:16 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 81 After sending out my mail about deleting or stripping characters out of a string I received a couple of responses from kindly Iconists. I later was showing off Icon to a friend when I notices a "delete" function in strutil.icn. Upon closer examination I found that delete also did what I wanted. The next step for me was to write a little benchmarking program so I could see how the different methods compared speedwise. It turns out that the IPL delete routine was the slowest, although the fastest was only 15 percent faster. Here is the test program: procedure main() test_string := " str ? while tab(upto(chars)) do text ||:= tab(many(chars))" remove := &cset -- &lcase time1 := &time limit := 1000 every 1 to limit do result1 := delete(test_string, remove) time2 := &time every 1 to limit do result2 := strip1(test_string, remove) time3 := &time every 1 to limit do result3 := strip2(test_string, remove) time4 := &time write(time1) write(time2-time1, " ", result1) write(time3-time2, " ", result2) write(time4-time3, " ", result3) return end # from IPL strutil.icn # delete characters # procedure delete(s,c) local i while i := upto(c,s) do s[i:many(c,s,i)] := "" return s end #From: Richard Goerwitz procedure strip1(s,c) s2 := "" s ? { while s2 ||:= tab(upto(c)) do tab(many(c)) s2 ||:= tab(0) } return s2 end #From: Chris Tenaglia - 257-8765 procedure strip2(str,chrs) local text,chars # /chrs := ' ' # (I commmented this out because the others don't do such checks.) chars := &cset -- chrs text := "" str ? while tab(upto(chars)) do text ||:= tab(many(chars)) return text end And the output (first column is time, second column is the result string which is always supposed to be the same). Initial time is 0, other times are in milliseconds. 0 21033 strwhiletabuptocharsdotexttabmanychars 20350 strwhiletabuptocharsdotexttabmanychars 17717 strwhiletabuptocharsdotexttabmanychars All three are reasonable, but the differences in approach are educational. o o \_____/ /-o-o-\ _______ DDDD SSSS CCCC / ^ \ /\\\\\\\\ D D S C \ \___/ / /\ ___ \ D D SSS C \_ _/ /\ /\\\\ \ D D S C \ \__/\ /\ @_/ / DDDDavid SSSS. CCCCargo \____\____\______/ CARGO@TARDIS.CRAY.COM