Created
February 5, 2019 20:06
-
-
Save WaKeMaTTa/0c54e5eaa4e6e647772f49fa5f92f744 to your computer and use it in GitHub Desktop.
Benchmark about "recursive regex match?" vs "recursive my own match?"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/inline' | |
gemfile(true) do | |
ruby '~> 2.5' | |
source 'https://rubygems.org' | |
gem 'benchmark-ips' | |
end | |
require 'benchmark/ips' | |
BALANCED = '(a)(b)((c))'.freeze | |
UNBALANCED = '(a)(b)(c))'.freeze | |
COMPLEX_REGEX = /\A(?<paren>\([^()]*\g<paren>*\))*\z/x.freeze | |
SIMPLE_REGEX = /(\([^()]\))+/x.freeze | |
def my_match?(string) | |
string = string.dup | |
captured = string.gsub!(SIMPLE_REGEX, '') | |
# string.strip! | |
if captured.nil? | |
true | |
elsif captured == '' | |
false | |
else | |
my_match?(string) | |
end | |
end | |
Benchmark.ips do |x| | |
x.report('recursive regex using ruby match? method - positive case') do | |
BALANCED.match?(COMPLEX_REGEX) | |
end | |
x.report('recursive regex using ruby match? method - negative case') do | |
UNBALANCED.match?(COMPLEX_REGEX) | |
end | |
x.report('recursive method using my_match? method - positive case') do | |
my_match?(BALANCED) | |
end | |
x.report('recursive method using my_match? method - negative case') do | |
my_match?(UNBALANCED) | |
end | |
x.compare! | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Fetching gem metadata from https://rubygems.org/. | |
Resolving dependencies... | |
Using benchmark-ips 2.7.2 | |
Using bundler 1.17.2 | |
Warming up -------------------------------------- | |
recursive regex using ruby match? method - positive case | |
122.483k i/100ms | |
recursive regex using ruby match? method - negative case | |
128.298k i/100ms | |
recursive method using my_match? method - positive case | |
20.203k i/100ms | |
recursive method using my_match? method - negative case | |
20.465k i/100ms | |
Calculating ------------------------------------- | |
recursive regex using ruby match? method - positive case | |
2.032M (± 2.1%) i/s - 10.166M in 5.004149s | |
recursive regex using ruby match? method - negative case | |
1.975M (± 3.1%) i/s - 9.879M in 5.006411s | |
recursive method using my_match? method - positive case | |
229.712k (± 5.3%) i/s - 1.152M in 5.026184s | |
recursive method using my_match? method - negative case | |
231.356k (± 5.1%) i/s - 1.167M in 5.054504s | |
Comparison: | |
recursive regex using ruby match? method - positive case: 2032494.3 i/s | |
recursive regex using ruby match? method - negative case: 1975252.4 i/s - same-ish: difference falls within error | |
recursive method using my_match? method - negative case: 231355.9 i/s - 8.79x slower | |
recursive method using my_match? method - positive case: 229711.9 i/s - 8.85x slower |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment