Skip to content

Instantly share code, notes, and snippets.

@clifff
Created November 29, 2012 18:39
Show Gist options
  • Save clifff/4171017 to your computer and use it in GitHub Desktop.
Save clifff/4171017 to your computer and use it in GitHub Desktop.
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