Skip to content

Instantly share code, notes, and snippets.

@bryckbost
Created April 27, 2011 20:46
Show Gist options
  • Save bryckbost/945168 to your computer and use it in GitHub Desktop.
Save bryckbost/945168 to your computer and use it in GitHub Desktop.
match versus =~.rb
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
100_000.times do
@string.match(/123/)
end
end
x.report("String#=~") do
100_000.times do
@string =~ /123/
end
end
x.report("Regexp#match") do
100_000.times do
/123/.match(@string)
end
end
x.report("Regexp#=~") do
100_000.times do
/123/ =~ @string
end
end
end
# Rehearsal ------------------------------------------------
# String#match 0.170000 0.010000 0.180000 ( 0.175012)
# String#=~ 0.050000 0.000000 0.050000 ( 0.045737)
# Regexp#match 0.160000 0.000000 0.160000 ( 0.159869)
# Regexp#=~ 0.040000 0.000000 0.040000 ( 0.045148)
# --------------------------------------- total: 0.430000sec
#
# user system total real
# String#match 0.170000 0.000000 0.170000 ( 0.165692)
# String#=~ 0.040000 0.000000 0.040000 ( 0.045257)
# Regexp#match 0.160000 0.000000 0.160000 ( 0.154075)
# Regexp#=~ 0.040000 0.000000 0.040000 ( 0.044705)
#
# Total: 82 samples
# 35 42.7% 42.7% 35 42.7% Regexp#match
# 25 30.5% 73.2% 60 73.2% Integer#times
# 21 25.6% 98.8% 21 25.6% garbage_collector
# 1 1.2% 100.0% 1 1.2% Process.times
# 0 0.0% 100.0% 61 74.4% Benchmark#bmbm
# 0 0.0% 100.0% 61 74.4% Benchmark#measure
# 0 0.0% 100.0% 1 1.2% Module#times
# 0 0.0% 100.0% 18 22.0% String#match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment