Skip to content

Instantly share code, notes, and snippets.

@y-yu
Created August 19, 2013 13:53
Show Gist options
  • Save y-yu/6269366 to your computer and use it in GitHub Desktop.
Save y-yu/6269366 to your computer and use it in GitHub Desktop.
Rubyの正規表現のベンチマーク (参考:http://blog.livedoor.jp/dankogai/archives/50533165.html) (ruby 2.1.0dev (2013-08-19 trunk 42614) [x86_64-darwin12.4.0])
require 'benchmark'
re_alt = Regexp.compile('sa|st|tw|tu')
re_opt = Regexp.compile('s[at]|t[wu]')
match = 'abcdefghijklmnopqrstuvwxyz'
not_match = 'sssstttttttttssssssssuuuuu'
ntimes = 100000;
Benchmark.bm(8) do |x|
x.report('alt') { ntimes.times{ re_alt.match(match) } }
x.report('opt') { ntimes.times{ re_opt.match(match) } }
end
Benchmark.bm(8) do |x|
x.report('alt') { ntimes.times{ re_alt.match(not_match) } }
x.report('opt') { ntimes.times{ re_opt.match(not_match) } }
end
user system total real
alt 0.140000 0.010000 0.150000 ( 0.142298)
opt 0.130000 0.000000 0.130000 ( 0.130673)
user system total real
alt 0.150000 0.000000 0.150000 ( 0.146799)
opt 0.130000 0.000000 0.130000 ( 0.139638)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment