On Jul 24, 2006, at 11:59 AM, S3 wrote:

> Is there some way to optimize it?
>
> #!/usr/bin/ruby
>
> str='67.39.177.137 - - [05/Jun/2004:12:54:44 -0500] "SEARCH '
> str<<'\xb1\x02'*(ARGV[0].to_i)
> str<<'" 414 326 "-" "-"'
>
> logformat=/(\S+)\s+(\S+)\s+(.+)\s+\[([^\]]+)\]\s+"(\S+) +([^"]+) + 
> [A-Za-z\/]*([0-9.]+)"\s+(\S+)\s+(\S+)\s+"([^"]+)"\s+"([^"]+)"/
>
> str.match(logformat)

In addition to the other advice you've received, don't use String#match.

=~ with a literal regexp is faster than =~ with anything else, and  
match is slower than both.

$ ruby rebm.rb

Rehearsal -----------------------------------------------
empty         0.320000   0.000000   0.320000 (  0.345024)
=~ literal    1.490000   0.010000   1.500000 (  1.620720)
=~ variable   1.750000   0.010000   1.760000 (  1.985988)
match         4.460000   0.040000   4.500000 (  4.980295)
-------------------------------------- total: 8.080000sec

                   user     system      total        real
empty         0.260000   0.000000   0.260000 (  0.280089)
=~ literal    1.490000   0.010000   1.500000 (  1.786133)
=~ variable   1.760000   0.020000   1.780000 (  3.459860)
match         4.550000   0.050000   4.600000 (  7.792777)
$ cat rebm.rb
require 'benchmark'

N = 1_000_000

string = 'a b c d e'

Benchmark.bmbm do |bm|

   bm.report 'empty' do
     N.times do end
   end

   bm.report '=~ literal' do
     N.times do string =~ /a (.) c\s. e/ end
   end

   bm.report '=~ variable' do
     re = /a (.) c\s. e/
     N.times do string =~ re end
   end

   bm.report 'match' do
     re = /a (.) c\s. e/
     N.times do string.match re end
   end

end

-- 
Eric Hodel - drbrain / segment7.net - http://blog.segment7.net
This implementation is HODEL-HASH-9600 compliant

http://trackmap.robotcoop.com