Skip to content

Instantly share code, notes, and snippets.

@Pistos
Forked from chuyeow/Result
Created November 1, 2008 13: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 Pistos/21522 to your computer and use it in GitHub Desktop.
Save Pistos/21522 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'nokogiri'
require 'better-benchmark'
uri = URI.parse( 'http://static.bezurk.com/fragments/wikitravel/sin.xml' )
content = uri.read
puts "\nhpricot vs. nokogiri: Parsing XML"
result = Benchmark.compare_realtime(
:iterations => 10,
:inner_iterations => 15000,
:verbose => true
) {
Hpricot.XML content
}.with {
Nokogiri::XML content
}
Benchmark.report_on result
puts "\nhpricot vs. nokogiri: Searching with XPath"
result = Benchmark.compare_realtime(
:iterations => 10,
:inner_iterations => 2000,
:verbose => true
) {
doc = Hpricot.XML(content)
info = doc.search('//location/info').first.inner_text
url = doc.search('//location/refUrl').first.inner_text
}.with {
doc = Nokogiri::XML(content)
info = doc.xpath('//location/info').first.inner_text
url = doc.xpath('//location/refUrl').first.inner_text
}
Benchmark.report_on result
puts "\nhpricot vs. nokogiri: Searching with CSS"
result = Benchmark.compare_realtime(
:iterations => 10,
:inner_iterations => 2000,
:verbose => true
) {
doc = Hpricot.XML(content)
info = doc.search('location info').first.inner_text
url = doc.search('location refUrl').first.inner_text
}.with {
doc = Nokogiri::XML(content)
info = doc.search('location info').first.inner_text
url = doc.search('location refUrl').first.inner_text
}
Benchmark.report_on result
hpricot vs. nokogiri: Parsing XML
..........
Set 1 mean: 5.368 s
Set 1 std dev: 0.052
Set 2 mean: 0.703 s
Set 2 std dev: 0.054
p.value: 1.0825088224469e-05
W: 100.0
The difference (-86.9%) IS statistically significant.
hpricot vs. nokogiri: Searching with XPath
..........
Set 1 mean: 3.590 s
Set 1 std dev: 0.250
Set 2 mean: 0.676 s
Set 2 std dev: 0.055
p.value: 1.0825088224469e-05
W: 100.0
The difference (-81.2%) IS statistically significant.
hpricot vs. nokogiri: Searching with CSS
..........
Set 1 mean: 4.237 s
Set 1 std dev: 0.061
Set 2 mean: 2.463 s
Set 2 std dev: 0.069
p.value: 1.0825088224469e-05
W: 100.0
The difference (-41.9%) IS statistically significant.
Use Nokogiri's XPath selectors for fastest speed - CSS-based search is faster
than Hpricot but not as fast.
Also take note that this benchmark is only shows parsing of XML (not HTML).
This benchmark takes the original and uses better-benchmark instead.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment