From: Devin Mullins Date: 2006-09-05T23:10:11+09:00 Subject: Re: Benchmarking (Was: Re: Compiling Regexp only once) Eric Hodel wrote: > So literal regular expressions are "faster" by about 56% (after > discounting loop overhead) (when you're performing 100 million > retrievals only) (actual speedup in real code will probably be > swallowed elsewhere or completely irrelevant). > > This follows from reading eval.c. Constant lookup involves a bunch of > C function calls, but literal lookup just returns an Object stored in > the parse tree. Interesting. Note that this is different from your benchmark in ruby-talk:204747, wherein much of the savings seems to come from not having to instantiate & populate a MatchData. Compare: 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 'none' do N.times do string =~ /a (.) c\s. e/ end end bm.report '5' do N.times do string =~ /a (.) c\s. e/;5 end end bm.report '$1' do N.times do string =~ /a (.) c\s. e/;$1 end end bm.report '$&' do N.times do string =~ /a (.) c\s. e/;$& end end bm.report '$~' do N.times do string =~ /a (.) c\s. e/;$~ end end bm.report 'match' do N.times do string.match /a (.) c\s. e/ end end end __END__ Rehearsal ----------------------------------------- empty 0.141000 0.000000 0.141000 ( 0.204000) none 1.453000 0.000000 1.453000 ( 1.500000) 5 1.578000 0.000000 1.578000 ( 1.625000) $1 3.500000 0.015000 3.515000 ( 3.546000) $& 3.640000 0.000000 3.640000 ( 3.875000) $~ 4.641000 0.016000 4.657000 ( 4.922000) match 5.313000 0.015000 5.328000 ( 5.422000) ------------------------------- total: 20.312000sec user system total real empty 0.156000 0.000000 0.156000 ( 0.156000) none 1.484000 0.000000 1.484000 ( 1.531000) 5 1.563000 0.000000 1.563000 ( 1.610000) $1 3.468000 0.000000 3.468000 ( 3.937000) $& 3.484000 0.000000 3.484000 ( 3.875000) $~ 4.609000 0.016000 4.625000 ( 4.641000) match 5.219000 0.016000 5.235000 ( 5.313000) Devin