Skip to content

Instantly share code, notes, and snippets.

@charlenopires
Forked from chuyeow/Result
Created November 21, 2009 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charlenopires/240245 to your computer and use it in GitHub Desktop.
Save charlenopires/240245 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'nokogiri'
require 'benchmark'
uri = URI.parse('http://static.bezurk.com/fragments/wikitravel/sin.xml')
content = uri.read
N = 1000
Benchmark.bm do |x|
x.report('hpricot:doc') do
N.times do
Hpricot.XML(content)
end
end
x.report('nokogiri:doc') do
N.times do
Nokogiri::XML(content)
end
end
end
Benchmark.bm do |x|
x.report('hpricot:xpath') do
N.times do
doc = Hpricot.XML(content)
info = doc.search('//location/info').first.inner_text
url = doc.search('//location/refUrl').first.inner_text
end
end
x.report('nokogiri:xpath') do
N.times do
doc = Nokogiri::XML(content)
info = doc.xpath('//location/info').first.inner_text
url = doc.xpath('//location/refUrl').first.inner_text
end
end
end
Benchmark.bm do |x|
x.report('hpricot:css') do
N.times do
doc = Hpricot.XML(content)
info = doc.search('location info').first.inner_text
url = doc.search('location refUrl').first.inner_text
end
end
x.report('nokogiri:css') do
N.times do
doc = Nokogiri::XML(content)
info = doc.search('location info').first.inner_text
url = doc.search('location refUrl').first.inner_text
end
end
end
$ ./hpricot_vs_nokogiri.rb
user system total real
hpricot:doc 0.230000 0.010000 0.240000 ( 0.265904)
nokogiri:doc 0.030000 0.010000 0.040000 ( 0.041641)
user system total real
hpricot:xpath 1.050000 0.020000 1.070000 ( 1.114454)
nokogiri:xpath 0.210000 0.010000 0.220000 ( 0.226418)
user system total real
hpricot:css 1.140000 0.030000 1.170000 ( 1.339635)
nokogiri:css 0.700000 0.010000 0.710000 ( 0.835559)
Use Nokogiri's XPath selectors for fastest speed - CSS-based search is faster than Hpricot but not as fast.
Also take note that this benchmark is only shows parsing of XML (not HTML).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment