Skip to content

Instantly share code, notes, and snippets.

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 maciejkowalski/49a0d45cf62da2f7403a to your computer and use it in GitHub Desktop.
Save maciejkowalski/49a0d45cf62da2f7403a to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'nokogiri'
require 'benchmark'
url = 'http://stackoverflow.com/questions/8214299/xpath-or-css-parsing-faster-for-nokogiri-on-html-files'
page = Nokogiri::HTML(open(url))
Benchmark.bmbm do |parser|
parser.report('Xpath') { 1000.times do page.at_xpath('//div[@id="sidebar"]//a[@class="question-hyperlink"]').text end }
parser.report('CSS') { 1000.times do page.at_css('#sidebar a.question-hyperlink').text end }
end
another_url = 'https://plus.google.com/+marriott/posts'
another_page = Nokogiri::HTML(open(another_url))
Benchmark.bmbm do |parser|
parser.report('Xpath') { 1000.times do another_page.at_xpath('//span[@class="K9a"]/a').text end }
parser.report('CSS') { 1000.times do another_page.at_css('span.K9a a').text end }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment