Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active February 6, 2016 11:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/10515805 to your computer and use it in GitHub Desktop.
Save komasaru/10515805 to your computer and use it in GitHub Desktop.
Ruby script to do tests of parsing xml with Nokogiri, Hpricot parsers.
# **************************************
# 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