Skip to content

Instantly share code, notes, and snippets.

@weppos
Created January 22, 2010 15:17
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 weppos/283825 to your computer and use it in GitHub Desktop.
Save weppos/283825 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'benchmark'
require 'nokogiri'
TIMES = 50_000
doc = Nokogiri::XML(File.new("test.xml"))
node = doc.at_xpath("./strategies")
Benchmark.bmbm do |x|
x.report("xpath/first()") do
TIMES.times { e = node.at_xpath("./strategy[1]") }
end
x.report("xpath.first") do
TIMES.times { e = node.xpath("./strategy").first }
end
x.report("at") do
TIMES.times { e = node.at("strategy") }
end
x.report("at_xpath") do
TIMES.times { e = node.at_xpath("./strategy") }
end
x.report("children.second") do # first returns a Node::Text
TIMES.times { e = node.children[1] }
end
end
__END__
$ ruby test.rb
Rehearsal ---------------------------------------------------
xpath/first() 3.290000 0.030000 3.320000 ( 3.321197)
xpath.first 3.360000 0.010000 3.370000 ( 3.381171)
at 4.540000 0.020000 4.560000 ( 4.564249)
at_xpath 3.420000 0.010000 3.430000 ( 3.430933)
children.second 0.220000 0.010000 0.230000 ( 0.233090)
----------------------------------------- total: 14.910000sec
user system total real
xpath/first() 3.280000 0.000000 3.280000 ( 3.288647)
xpath.first 3.350000 0.020000 3.370000 ( 3.374778)
at 4.530000 0.040000 4.570000 ( 4.580512)
at_xpath 3.410000 0.010000 3.420000 ( 3.421551)
children.second 0.220000 0.010000 0.230000 ( 0.226846)
<strategies type="array">
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
<strategy>
<name>string</name>
<path>string</path>
</strategy>
</strategies>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment