Skip to content

Instantly share code, notes, and snippets.

@mrzasa
Created September 10, 2018 14:25
Show Gist options
  • Save mrzasa/52e22b94fa93b0d637904ff3b06eebae to your computer and use it in GitHub Desktop.
Save mrzasa/52e22b94fa93b0d637904ff3b06eebae to your computer and use it in GitHub Desktop.
textmaster-medium-regex-perfomance-benchmark
require "benchmark/ips"
PART = <<TEXT
A text with 123,21231,231,23d a number: 12,212,234,12
Other 12 43 123,21231,22,22,2,33d Some other 9012,123123 12
TEXT
TEXT = PART * 100
POSSESSIVE = /(\d++,?)++/
LAZY = /(\d+?,?)+?/
NORMAL = /(\d+,?)+/
UNROLLING = /-?\d+(,\d+)*/
def count(string, regex)
string.scan(regex).count
end
def compile_regex(regex)
/(?<=\s)(#{regex})(?=\s)/
end
POSSESSIVE_WHOLE = compile_regex(POSSESSIVE)
LAZY_WHOLE = compile_regex(LAZY)
NORMAL_WHOLE = compile_regex(NORMAL)
UNROLLING_WHOLE = compile_regex(UNROLLING)
def measure(string, x)
x.report('possessive') { count(string, POSSESSIVE_WHOLE) }
x.report('lazy') { count(string, LAZY_WHOLE) }
x.report('greedy') { count(string, NORMAL_WHOLE) }
x.report('unrolling') { count(string, UNROLLING_WHOLE) }
x.compare!
end
def benchmark(string)
Benchmark.ips do |x|
measure(string, x)
end
end
benchmark(TEXT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment