Skip to content

Instantly share code, notes, and snippets.

@mcansky
Created July 12, 2012 15:35
Show Gist options
  • Select an option

  • Save mcansky/3098899 to your computer and use it in GitHub Desktop.

Select an option

Save mcansky/3098899 to your computer and use it in GitHub Desktop.
ruby eq strings
require 'benchmark'
N = 10000
STRING = "cat"
DAT = "cat"
puts "#{STRING} vs #{DAT}"
Benchmark.bmbm do |x|
x.report("==") { N.times {
"cat" == "cat"
} }
x.report("== const") { N.times {
DAT == STRING
} }
x.report("regexp") { N.times {
DAT =~ /^cat$/
} }
x.report("match") { N.times {
DAT.match STRING
} }
end
# user system total real
# == 0.000000 0.000000 0.000000 ( 0.002899)
# == const 0.010000 0.000000 0.010000 ( 0.001940)
# regexp 0.000000 0.000000 0.000000 ( 0.005289)
# match 0.020000 0.000000 0.020000 ( 0.013405)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment