From: Stefan Lang Date: 2005-01-11T01:51:27+09:00 Subject: Re: brute force string search Michael C. Libby wrote: > On Mon, 2005-01-10 at 09:51 +0900, Martin Pirker wrote: >> Hi... >> >> given: String of several Mb >> problem: find the lines in String containing "xyz" >> >> >> Idea 1: >> String.scan(/.*xyz.*/) -> ~10s runtime >> >> 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 > > Did you try something like this? > > irb(main):003:0> big_string.each do |x| puts x if x=~/xyz/ end Perhaps faster with: ... if x.include? "xyz" > eeexyzfff > qqqqxyzqqq > => "abc\neeexyzfff\naadfwe\nqqqqxyzqqq\n" > irb(main):004:0> big_string.split.grep(/xyz/) > => ["eeexyzfff", "qqqqxyzqqq"] > irb(main):005:0> > > Intuitively it seems to me that wildcards (especially unanchored) in REs > will slow them down. > > -Michael -- Stefan