Skip to content

Instantly share code, notes, and snippets.

@codatory
Created August 12, 2011 14:18
Show Gist options
  • Save codatory/1142122 to your computer and use it in GitHub Desktop.
Save codatory/1142122 to your computer and use it in GitHub Desktop.
# Handy Benchmark Script for comparing times of two methods
require 'benchmark'
require './lib/regexp'
# Uses https://github.com/mpalmer/email-address-validator
times = 20000000
email_1 = 'jdoe@gmail.com'
email_2 = 'foo.bar'
email_3 = 'nframe.com'
match = Benchmark::Tms.new
tilde = Benchmark::Tms.new
Benchmark.bm(times) do |x|
match = x.report("String.match"){
email_1.match(Regexp::ADDR_SPEC)
email_2.match(Regexp::ADDR_SPEC)
email_3.match(Regexp::ADDR_SPEC)
}
tilde = x.report("=~"){
Regexp::ADDR_SPEC =~ email_1
Regexp::ADDR_SPEC =~ email_2
Regexp::ADDR_SPEC =~ email_3
}
end
puts 'Total time for Match'
puts match * times
puts 'Total time for =~'
puts tilde * times
puts "\n\n"
slower_time = match.to_a[5]
faster_time = tilde.to_a[5]
puts "#{(((slower_time - faster_time) / slower_time)*100).round} % Faster"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment