Skip to content

Instantly share code, notes, and snippets.

@clifff clifff/gist:4171017
Created Nov 29, 2012

Embed
What would you like to do?
URI.parse vs regex
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
You can’t perform that action at this time.