Skip to content

Instantly share code, notes, and snippets.

@ixti
Created July 3, 2013 15:37
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 ixti/c78acb425808c3ddb326 to your computer and use it in GitHub Desktop.
Save ixti/c78acb425808c3ddb326 to your computer and use it in GitHub Desktop.
require "nokogiri"
require "benchmark"
doc = Nokogiri.XML <<-XML
<Listings>
<Listing>
<Location>
<StreetAddress>123 Main St</StreetAddress>
<UnitNumber>2F</UnitNumber>
<City>Anytown</City>
<State>NY</State>
<Zip>10000</Zip>
</Location>
</Listing>
<property>
<Location>
<StreetAddress>321 Main St</StreetAddress>
<UnitNumber>2F</UnitNumber>
<City>Anytown</City>
<State>NY</State>
<Zip>10000</Zip>
</Location>
</property>
</Listings>
XML
ITERATIONS = 1000
Benchmark.bmbm do |x|
x.report("xpath") do
ITERATIONS.times do
doc.xpath("//*[self::Location or self::property]").map do |item|
item.xpath("./*[self::StreetAddress or self::AlternativeStreetAddress]").text
end
end
end
x.report("css") do
ITERATIONS.times do
doc.css("Location,property").map do |item|
item.css("StreetAddress,AlternativeStreetAddress").text
end
end
end
x.report("find") do
ITERATIONS.times do
doc.search("Location", "property").map do |item|
item.at("StreetAddress", "AlternativeStreetAddress").text
end
end
end
end
@ixti
Copy link
Author

ixti commented Jul 3, 2013

% ruby test.rb
Rehearsal -----------------------------------------
xpath   0.340000   0.000000   0.340000 (  0.336482)
css     0.620000   0.000000   0.620000 (  0.621865)
find    0.660000   0.000000   0.660000 (  0.655835)
-------------------------------- total: 1.620000sec

            user     system      total        real
xpath   0.320000   0.000000   0.320000 (  0.321618)
css     0.620000   0.000000   0.620000 (  0.621304)
find    0.650000   0.000000   0.650000 (  0.641275)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment