Last active
February 6, 2016 11:38
-
-
Save komasaru/10515805 to your computer and use it in GitHub Desktop.
Ruby script to do tests of parsing xml with Nokogiri, Hpricot parsers.
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
# ************************************** | |
# XML パーステスト | |
# ************************************** | |
# | |
require 'hpricot' | |
require 'nokogiri' | |
class TestXml | |
FILE_XML = "sitemap.xml" | |
N = 100 | |
def proc_main | |
begin | |
# Nokogiri | |
t1 = Time.now | |
N.times { |i| parse_nokogiri } | |
t2 = Time.now | |
printf("[Nokogiri] Time: %6.2f sec.\n", t2 - t1) | |
# Hpricot | |
t1 = Time.now | |
N.times { |i| parse_hpricot } | |
t2 = Time.now | |
printf("[Hpricot ] Time: %6.2f sec.\n", t2 - t1) | |
rescue => e | |
puts "[#{e.class}] #{e.message}" | |
e.backtrace.each{|trace| puts "\t#{trace}"} | |
exit | |
end | |
end | |
private | |
def parse_nokogiri | |
begin | |
doc = Nokogiri::XML(File.read(FILE_XML)) | |
ns = {"ns" => "http://www.sitemaps.org/schemas/sitemap/0.9"} | |
#locs = doc.css('urlset url loc') # <= 遅い | |
locs = doc.xpath('/ns:urlset/ns:url/ns:loc', ns) | |
rescue => e | |
raise | |
end | |
end | |
def parse_hpricot | |
begin | |
doc = Hpricot::XML(File.read(FILE_XML)) | |
#locs = doc/:urlset/:url/:loc # <= 遅い | |
#locs = doc/('urlset url loc') # <= 遅い | |
locs = doc/('/urlset/url/loc') | |
rescue => e | |
raise | |
end | |
end | |
end | |
TestXml.new.proc_main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment