Skip to content

Instantly share code, notes, and snippets.

@3dd13
Created November 5, 2010 04:00
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 3dd13/663626 to your computer and use it in GitHub Desktop.
Save 3dd13/663626 to your computer and use it in GitHub Desktop.
just curious about CSS and XPath selector performance. did some benchmarking
require 'rubygems'
require 'mechanize'
require 'benchmark'
agent = Mechanize.new
# however, this fetch already spend around 2 seconds. one http request response.
page = agent.get('http://www.ufood.com.hk/search/search.action?name=&distIds=4,10,11,3,5,8,6,7,2,1,9,12,13,14,15,18,16,17,19,20,21,22,23,24,25,26,33,34,27,35,32,30,29,28,31&ftIds=')
Benchmark.bm do |b|
# search by tag name and class 10.470000 0.040000 10.510000 ( 11.149709)
b.report("search by tag name and class") {
5_000.times {
page.search('div.footer')
}
}
# search by xpath 0.890000 0.030000 0.920000 ( 0.967669)
b.report("search by xpath") {
5_000.times {
page.search('//center/div[4]')
}
}
# search by full xpath 0.330000 0.020000 0.350000 ( 0.359641)
b.report("search by full xpath") {
5_000.times {
page.search('/html/body/center/div[4]')
}
}
# search by class 39.860000 0.150000 40.010000 ( 44.043039)
b.report("search by class") {
5_000.times {
page.search('.footer')
}
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment