Skip to content

Instantly share code, notes, and snippets.

@PatrickTulskie
Created October 5, 2012 18:12
Show Gist options
  • Save PatrickTulskie/3841426 to your computer and use it in GitHub Desktop.
Save PatrickTulskie/3841426 to your computer and use it in GitHub Desktop.
String#include? vs String#match
require "benchmark"
n = 1000000
puts "Running include? vs regex 1 million times.\n"
Benchmark.bmbm do |results|
results.report("include? ") { n.times { "banana".include?('nana') } }
results.report("match w/ regex") { n.times { "banana".match(/nana/) } }
end
=begin
Running include? vs regex 1 million times.
Rehearsal --------------------------------------------------
include? 0.230000 0.000000 0.230000 ( 0.226732)
match w/ regex 0.920000 0.000000 0.920000 ( 0.919457)
----------------------------------------- total: 1.150000sec
user system total real
include? 0.220000 0.000000 0.220000 ( 0.222755)
match w/ regex 0.930000 0.000000 0.930000 ( 0.933484)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment