Skip to content

Instantly share code, notes, and snippets.

@miry
Last active December 14, 2015 21:19
Show Gist options
  • Save miry/5150174 to your computer and use it in GitHub Desktop.
Save miry/5150174 to your computer and use it in GitHub Desktop.
require 'benchmark'
n = 50000000
Benchmark.bm(15) do |x|
x.report("regexp1") { 1.upto(n) do ; "XMLHttpReQUEst" =~ /XMLHttpRequest/i; end }
x.report("regexp2") { 1.upto(n) do ; "XMLHttpReQUEst" =~ /xmlhttprequest/i; end }
x.report("downcase") { 1.upto(n) do ; 'XMLHttpReQUEst'.downcase == 'xmlhttprequest'; end }
x.report("strip+downcase") { 1.upto(n) do ; ' XMLHttpReQUEst '.strip!.downcase! == 'xmlhttprequest'; end }
end
=begin
$ ruby-1.9.3-p327 regexp_vs_downcase.rb
user system total real
regexp1 123.980000 0.460000 124.440000 (133.326724)
regexp2 122.140000 0.290000 122.430000 (125.875973)
downcase 96.760000 0.150000 96.910000 ( 97.841658)
strip+downcase 104.450000 0.240000 104.690000 (108.681858)
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment