DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Benchmarking Ruby Regexes: Lazy Versus Not
// A benchmark for figuring out which is faster -- a lazy match (*?) or a negated character class ([^x]*)
require 'benchmark'
include Benchmark
LOOP_COUNT = 100
STRING = []
STRING << "ab" * 5_000
STRING << ("a"* 9_999) + "b"
STRING << "a" * 10_000
STRING << "b" * 10_000
def do_regex_lazy(i)
STRING[i].match(/a.*?b/)
nil
end
def do_regex_not(i)
STRING[i].match(/a[^b]*b/)
nil
end
def hr
puts "-" * 40
end
# This will take a while to run on a normal PC.
bm(12) do |test|
hr
test.report("short lazy match:") {LOOP_COUNT.times {do_regex_lazy 0}}
test.report("short not match:") {LOOP_COUNT.times { do_regex_not 0}}
hr
test.report("long lazy match:") {LOOP_COUNT.times {do_regex_lazy 1}}
test.report("long not match:") {LOOP_COUNT.times { do_regex_not 1}}
hr
test.report("missing lazy match:") {LOOP_COUNT.times {do_regex_lazy 2}}
test.report("missing not match:") {LOOP_COUNT.times { do_regex_not 2}}
hr
test.report("missing lazy match 2:") {LOOP_COUNT.times {do_regex_lazy 3}}
test.report("missing not match 2:") {LOOP_COUNT.times { do_regex_not 3}}
hr
test.report("lazy (all):") {LOOP_COUNT.times {4.times {|i| do_regex_lazy i}}}
test.report("not (all):") {LOOP_COUNT.times {4.times {|i| do_regex_not i}}}
end
Results:
user system total real
----------------------------------------
short lazy match: 0.000000 0.000000 0.000000 ( 0.000244)
short not match: 0.000000 0.000000 0.000000 ( 0.000232)
----------------------------------------
long lazy match: 0.050000 0.000000 0.050000 ( 0.052447)
long not match: 0.050000 0.000000 0.050000 ( 0.047669)
----------------------------------------
missing lazy match:254.640000 0.140000 254.780000 (255.988425)
missing not match:224.580000 0.130000 224.710000 (226.142944)
----------------------------------------
missing lazy match 2: 0.010000 0.000000 0.010000 ( 0.006238)
missing not match 2: 0.000000 0.000000 0.000000 ( 0.005571)
----------------------------------------





