Skip to content

Instantly share code, notes, and snippets.

@ryana

ryana/regex.perf.rb

Created Apr 12, 2010
Embed
What would you like to do?
Testing regex parsing/evaluation performance of Ruby regexes
#!/usr/bin/ruby
require 'benchmark'
ryan = /\ARyan/
blake = /\ABlake/
darren = /\ADarren/
devin = /\ADevin/
composite3_regex = /\A(Ryan|Blake|Darren)/
composite4_regex = /\A(Ryan|Blake|Darren|Devin)/
matching_string = "Ryan"
non_matching_string = "Matthew"
Benchmark.bmbm do |x|
iterations = 1000000
x.report('3 regex matching') do
iterations.times do
matching_string =~ ryan
matching_string =~ blake
matching_string =~ darren
end
end
x.report('4 regex matching') do
iterations.times do
matching_string =~ ryan
matching_string =~ blake
matching_string =~ darren
matching_string =~ devin
end
end
x.report('3 regex non-matching') do
iterations.times do
non_matching_string =~ ryan
non_matching_string =~ blake
non_matching_string =~ darren
end
end
x.report('4 regex non-matching') do
iterations.times do
non_matching_string =~ ryan
non_matching_string =~ blake
non_matching_string =~ darren
non_matching_string =~ devin
end
end
x.report('composite(3) regex matching') do
iterations.times do
matching_string =~ composite3_regex
end
end
x.report('composite(3) regex non-matching') do
iterations.times do
non_matching_string =~ composite3_regex
end
end
x.report('composite(4) regex matching') do
iterations.times do
matching_string =~ composite4_regex
end
end
x.report('composite(4) regex non-matching') do
iterations.times do
non_matching_string =~ composite4_regex
end
end
end
Rehearsal -------------------------------------------------------------------
3 regex matching 2.630000 0.010000 2.640000 ( 2.709336)
4 regex matching 3.200000 0.010000 3.210000 ( 3.259288)
3 regex non-matching 1.940000 0.000000 1.940000 ( 1.951221)
4 regex non-matching 2.560000 0.010000 2.570000 ( 2.617336)
composite(3) regex matching 0.900000 0.000000 0.900000 ( 0.909685)
composite(3) regex non-matching 0.850000 0.000000 0.850000 ( 0.854737)
composite(4) regex matching 0.930000 0.000000 0.930000 ( 0.931522)
composite(4) regex non-matching 0.890000 0.000000 0.890000 ( 0.897464)
--------------------------------------------------------- total: 13.930000sec
user system total real
3 regex matching 2.620000 0.010000 2.630000 ( 2.699888)
4 regex matching 3.190000 0.010000 3.200000 ( 3.205196)
3 regex non-matching 2.080000 0.000000 2.080000 ( 2.102109)
4 regex non-matching 2.730000 0.010000 2.740000 ( 2.774280)
composite(3) regex matching 0.920000 0.000000 0.920000 ( 0.929301)
composite(3) regex non-matching 0.890000 0.010000 0.900000 ( 0.890297)
composite(4) regex matching 0.930000 0.000000 0.930000 ( 0.930574)
composite(4) regex non-matching 0.910000 0.010000 0.920000 ( 0.993974)
@dabble

This comment has been minimized.

Copy link

@dabble dabble commented Oct 1, 2011

Interestingly, in 1.9.2, if you inline the regular expressions, it's slightly faster.
matching_string =~ /\ARyan/
is faster than
matching_string =~ ryan

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment