Created
August 12, 2011 14:18
-
-
Save codatory/1142122 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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