Skip to content

Instantly share code, notes, and snippets.

@britishtea
Created October 13, 2014 13:43
Show Gist options
  • Save britishtea/5836046e747866d876e7 to your computer and use it in GitHub Desktop.
Save britishtea/5836046e747866d876e7 to your computer and use it in GitHub Desktop.
String#=~ vs String#include? vs String#[]
Benchmark.ips do |x|
x.report { x="str"; x =~ /foo/ || x =~ /bar/ }
x.report { x="str"; x.include?("foo") || x.include?("bar") }
x.report { x="str"; x["foo"] || x["bar"] }
end
Calculating -------------------------------------
25880 i/100ms
31953 i/100ms
32632 i/100ms
-------------------------------------------------
583736.1 (±5.3%) i/s - 2924440 in 5.025143s
775976.0 (±15.7%) i/s - 3642642 in 5.065275s
778926.8 (±10.5%) i/s - 3850576 in 5.010336s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment