Created
October 22, 2008 03:43
-
-
Save chuyeow/18533 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/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 |
This file contains hidden or 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
$ ./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) |
This file contains hidden or 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
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
a more up-to-date benchmark:
hpricot:doc 0.020000 0.000000 0.020000 ( 0.026553)
nokogiri:doc 0.210000 0.000000 0.210000 ( 0.210191)
user system total real
hpricot:xpath 0.720000 0.020000 0.740000 ( 0.742866)
nokogiri:xpath 0.560000 0.000000 0.560000 ( 0.558950)
user system total real
hpricot:css 0.840000 0.010000 0.850000 ( 0.878004)
nokogiri:css 0.600000 0.000000 0.600000 ( 0.614791)