Skip to content

Instantly share code, notes, and snippets.

@pointcom
Created October 21, 2008 20:06
Show Gist options
  • Save pointcom/18405 to your computer and use it in GitHub Desktop.
Save pointcom/18405 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'benchmark'
require 'rubygems'
puts "Chargement de la page HTML à parser..."
html = IO.read("page.html")
puts "## Récupérer tous les liens par selecteur XPath"
puts "## //table[1]/tbody/tr/td[1]/a"
Benchmark.bm do |x|
x.report("hpricot") {
require 'hpricot'
doc = Hpricot(html)
packages = (doc/"//table[1]/tbody/tr/td[1]/a").map {|link| link.inner_text }
}
x.report("nokogiri") {
require 'nokogiri'
doc = Nokogiri::HTML(html)
packages = (doc/"//table[1]/tbody/tr/td[1]/a").map {|link| link.inner_text }
}
end
puts "\n\n\n"
puts "## Récupérer une cellule du tableau spécifique par selecteur XPath"
puts "## //table[1]/tbody/tr/td[@sortable-data='1197241200000000']"
Benchmark.bm do |x|
x.report("hpricot") {
require 'hpricot'
doc = Hpricot(html)
date = (doc/"//table[1]/tbody/tr/td[@sortable-data='1197241200000000']").inner_text
}
x.report("nokogiri") {
require 'nokogiri'
doc = Nokogiri::HTML(html)
date = (doc/"//table[1]/tbody/tr/td[@sortable-data='1197241200000000']").inner_text
}
end
puts "\n\n\n"
puts "## Récupérer tous les liens par selecteur CSS"
puts "## a.symlink"
Benchmark.bm do |x|
x.report("hpricot") {
require 'hpricot'
doc = Hpricot(html)
packages = (doc/"a.symlink").map {|link| link.inner_text }
}
x.report("nokogiri") {
require 'nokogiri'
doc = Nokogiri::HTML(html)
packages = (doc/"a.symlink").map {|link| link.inner_text }
}
end
Chargement de la page HTML à parser...
## Récupérer tous les liens par selecteur XPath
## //table[1]/tbody/tr/td[1]/a
user system total real
hpricot 17.630000 0.510000 18.140000 ( 21.813587)
nokogiri 2.700000 0.100000 2.800000 ( 3.076731)
## Récupérer une cellule du tableau spécifique par selecteur XPath
## //table[1]/tbody/tr/td[@sortable-data='1197241200000000']
user system total real
hpricot 13.680000 0.150000 13.830000 ( 14.122796)
nokogiri 1.050000 0.020000 1.070000 ( 1.107357)
## Récupérer tous les liens par selecteur CSS
## a.symlink
user system total real
hpricot 10.680000 0.250000 10.930000 ( 11.144677)
nokogiri 3.930000 0.040000 3.970000 ( 4.035106)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment