Created
December 28, 2013 23:32
-
-
Save ericat/8165659 to your computer and use it in GitHub Desktop.
Quick benchmarking between Xpath and CSS selectors with Nokogiri.
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