Skip to content

Instantly share code, notes, and snippets.

@dmke
Created March 20, 2014 11:49
Show Gist options
  • Save dmke/9662059 to your computer and use it in GitHub Desktop.
Save dmke/9662059 to your computer and use it in GitHub Desktop.
$ ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-linux]
$ ruby re_bench.rb
Rehearsal --------------------------------------------------------
(?-mix:^[a-zA-Z]$) 0.780000 0.000000 0.780000 ( 0.783924)
(?i-mx:^[a-z]$) 0.790000 0.000000 0.790000 ( 0.789410)
(?-mix:\A[a-zA-Z]\z) 0.370000 0.000000 0.370000 ( 0.373894)
(?i-mx:\A[a-z]\z) 0.370000 0.000000 0.370000 ( 0.373349)
----------------------------------------------- total: 2.310000sec
user system total real
(?-mix:^[a-zA-Z]$) 0.770000 0.000000 0.770000 ( 0.773134)
(?i-mx:^[a-z]$) 0.790000 0.000000 0.790000 ( 0.787266)
(?-mix:\A[a-zA-Z]\z) 0.380000 0.000000 0.380000 ( 0.375982)
(?i-mx:\A[a-z]\z) 0.380000 0.000000 0.380000 ( 0.374873)
require 'benchmark'
N = 1_000_000
STRING = ['a'..'z', 'A'..'Z'].map(&:to_a).sample(2).join.freeze
def test_match(re)
STRING =~ re
end
CS_OLD = /^[a-zA-Z]$/
CIS_OLD = /^[a-z]$/i
CS_NEW = /\A[a-zA-Z]\z/
CIS_NEW = /\A[a-z]\z/i
Benchmark.bmbm(15) do |x|
[CS_OLD, CIS_OLD, CS_NEW, CIS_NEW].each do |re|
x.report(re.to_s) { N.times { test_match re } }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment