Forked from ericat/Xpath vs CSS Selectors in Ruby
Last active
August 29, 2015 14:11
-
-
Save maciejkowalski/49a0d45cf62da2f7403a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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