Created
November 29, 2012 18:39
-
-
Save clifff/4171017 to your computer and use it in GitHub Desktop.
URI.parse vs regex
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
require 'URI' | |
require 'benchmark' | |
include Benchmark | |
urls = %W{ | |
http://www.theverge.com/posts/comment | |
http://www.thefalcoholic.com/posts/ajax_comments_update/3468659?last_version=128 | |
http://www.polygon.com/game/dead-island-riptide/3635 | |
http://www.arrowheadpride.com/rss2/index.xml | |
http://www.broadstreethockey.com/ | |
} | |
Benchmark.bm do |b| | |
b.report("URI.parse") do | |
1000.times do | |
urls.each do |url| | |
URI.parse(url).host | |
end | |
end | |
end | |
# Rubular: http://rubular.com/r/H9bK6iAq67 | |
b.report("regex") do | |
1000.times do | |
urls.each do |url| | |
url =~ %r!http:\/\/www\..*\.\w{3}(.*)! | |
end | |
end | |
end | |
end | |
=begin | |
user system total real | |
URI.parse 0.180000 0.000000 0.180000 ( 0.249497) | |
regex 0.010000 0.000000 0.010000 ( 0.011040) | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment