Skip to content

Instantly share code, notes, and snippets.

@komasaru
Created August 28, 2015 06:11
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save komasaru/1f737b67baba38213570 to your computer and use it in GitHub Desktop.
Ruby script to verify the speed of xml parsing by Nokogiri.
#! /usr/local/bin/ruby
# coding: utf-8
#
# Ruby script to verify the speed of xml parsing by Nokogiri.
#
# Copyright(C) 2015 mk-mode.com All Rights Reserved.
#---------------------------------------------------------------------------------
#++
require 'benchmark'
require 'nokogiri'
class TestNokogiriXml
def initialize
@xml = Nokogiri::XML(File.read("atom/2015/08/27/20150827172859545_atom.xml"))
@secs = 10000
@ns = {"a" => "http://www.w3.org/2005/Atom"}
end
def exec
# 1. css メソッド(CSS セレクタ)による解析
printf("CASE-1: %.4f secs.\n", case_1)
# 2. search メソッド(CSS セレクタ)による解析
printf("CASE-2: %.4f secs.\n", case_2)
# 3. search メソッド(XPath, Namespace 指定なし)による解析
printf("CASE-3: %.4f secs.\n", case_3)
# 4. / メソッド(CSS セレクタ)による解析
printf("CASE-4: %.4f secs.\n", case_4)
# 5. / メソッド(XPath, Namespace 指定なし)による解析
printf("CASE-5: %.4f secs.\n", case_5)
# 6. xpath メソッド(XPath, Namespace 指定なし)による解析
printf("CASE-6: %.4f secs.\n", case_6)
# 7. xpath メソッド(XPath, Namespace 指定あり)による解析
printf("CASE-7: %.4f secs.\n", case_7)
rescue => e
$stderr.puts "[#{self.class.name}.#{__method__}] #{e}"
e.backtrace.each{ |tr| $stderr.puts "\t#{tr}" }
exit 1
end
private
def case_1
return Benchmark.realtime do
@secs.times do |i|
@xml.css("entry").each do |e|
title = e.css("title").first.text
end
end
end
rescue => e
raise
end
def case_2
return Benchmark.realtime do
@secs.times do |i|
@xml.search("entry").each do |e|
title = e.search("title").first.text
end
end
end
rescue => e
raise
end
def case_3
return Benchmark.realtime do
@secs.times do |i|
@xml.search("//xmlns:entry").each do |e|
title = e.search("./xmlns:title").first.text
end
end
end
rescue => e
raise
end
def case_4
return Benchmark.realtime do
@secs.times do |i|
(@xml/"entry").each do |e|
title = (e/"title").first.text
end
# 以下でも同様。しかし、ごく少し遅い。
#(@xml/:entry).each do |e|
# title = (e/:title).first.text
#end
end
end
rescue => e
raise
end
def case_5
return Benchmark.realtime do
@secs.times do |i|
(@xml/"//xmlns:entry").each do |e|
title = (e/"./xmlns:title").first.text
end
end
end
rescue => e
raise
end
def case_6
return Benchmark.realtime do
@secs.times do |i|
@xml.xpath("//xmlns:entry").each do |e|
title = e.xpath("xmlns:title").first.text
end
end
end
rescue => e
raise
end
def case_7
return Benchmark.realtime do
@secs.times do |i|
@xml.xpath("//a:entry", @ns).each do |e|
title = e.xpath("a:title", @ns).first.text
end
end
end
rescue => e
raise
end
end
TestNokogiriXml.new.exec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment