From: Charles Mills Date: 2005-01-10T15:16:22+09:00 Subject: Re: brute force string search 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 > Try the StringScanner library. It is very fast. ### require 'strscan' scanner = StringScanner.new(str, false) match = scanner.scan(/.*xyz.*/) ### you will have to change your regexp in order to skip lines not containing 'xyz' since it always matches from the current position in the string. Docs: http://i.loveruby.net/en/prog/strscan.html If you just want to count the number of lines you could use the regexp look ahead and look behind instead of '.*'. > > C extension the only faster option left? :-) > It is an option, but it might not be much faster. -Charlie