From: Robert Klemme Date: 2005-01-10T17:46:22+09:00 Subject: Re: brute force string search "Robert Klemme" schrieb im Newsbeitrag news:34es3pF48qpjkU1@individual.net... > > "Martin Pirker" schrieb im Newsbeitrag > news:41e1d123$0$11610$3b214f66@aconews.univie.ac.at... > > Hi... > > > > given: String of several Mb > > problem: find the lines in String containing "xyz" > > > > > > Idea 1: > > String.scan(/.*xyz.*/) -> ~10s runtime > > Did you try the anchored version? Did it make a difference? > > >> s = "a\nbxyz\ncxyz\nd" > => "a\nbxyz\ncxyz\nd" > >> s.scan(/^.*xyz.*$/) > => ["bxyz", "cxyz"] > > Did you try the block form, i.e., > > s.scan(/^.*xyz.*$/) {|m| # work with m} > > > Idea 2: > > String.grep(/.*xyz.*/) -> ~3s (but gives the \n too) > > > > Idea 3: > > loop String.index("xyz",lastmatch+3) > > loop results array and grep in match area for line > > -> 0,5s > > > > > > C extension the only faster option left? :-) > > Im not sure whether you will gain much, because certain things will have > to be done anyway: allocation of the result array, of the match strings > and expansion of the result array. > > Kind regards > > robert > Here are other options: >> s.inject([]){|ar,x| (x.chomp! ; ar << x) if /xyz/ =~ x; ar} => ["bxyz", "cxyz"] >> s.inject([]){|ar,x| (x.chomp! ; ar << x) if x.include? "xyz"; ar} => ["bxyz", "cxyz"] Cheers robert