Created
April 28, 2011 18:48
-
-
Save bryckbost/947005 to your computer and use it in GitHub Desktop.
Regex benchmark
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 'benchmark' | |
@string = "this is a long string that I want 123 to appear in" | |
Benchmark.bmbm(6) do |x| | |
x.report("String#match") do | |
1_000_000.times do | |
@string.match(/123/) | |
end | |
end | |
x.report("Regexp#match") do | |
1_000_000.times do | |
/123/.match(@string) | |
end | |
end | |
x.report("String#=~") do | |
1_000_000.times do | |
@string =~ /123/ | |
end | |
end | |
x.report("Regexp#=~") do | |
1_000_000.times do | |
/123/ =~ @string | |
end | |
end | |
x.report("String#[]") do | |
1_000_000.times do | |
@string[/123/] | |
end | |
end | |
end | |
# Rehearsal ------------------------------------------------ | |
# String#match 1.570000 0.000000 1.570000 ( 1.585916) | |
# Regexp#match 1.510000 0.000000 1.510000 ( 1.517587) | |
# String#=~ 0.450000 0.000000 0.450000 ( 0.446564) | |
# Regexp#=~ 0.440000 0.000000 0.440000 ( 0.447622) | |
# String#[] 0.590000 0.000000 0.590000 ( 0.588521) | |
# --------------------------------------- total: 4.560000sec | |
# | |
# user system total real | |
# String#match 1.570000 0.000000 1.570000 ( 1.580562) | |
# Regexp#match 1.510000 0.000000 1.510000 ( 1.512344) | |
# String#=~ 0.440000 0.000000 0.440000 ( 0.445906) | |
# Regexp#=~ 0.450000 0.000000 0.450000 ( 0.449013) | |
# String#[] 0.590000 0.000000 0.590000 ( 0.588304) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment