chuyeow (owner)

Forks

Revisions

gist: 18533 Download_button fork
public
Public Clone URL: git://gist.github.com/18533.git
Embed All Files: show embed
benchmark.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'nokogiri'
require 'benchmark'
 
uri = URI.parse('http://static.bezurk.com/fragments/wikitravel/sin.xml')
content = uri.read
 
N = 1000
 
Benchmark.bm do |x|
  x.report('hpricot:doc') do
    N.times do
      Hpricot.XML(content)
    end
  end
 
  x.report('nokogiri:doc') do
    N.times do
      Nokogiri::XML(content)
    end
  end
end
 
Benchmark.bm do |x|
  x.report('hpricot:xpath') do
    N.times do
      doc = Hpricot.XML(content)
      info = doc.search('//location/info').first.inner_text
      url = doc.search('//location/refUrl').first.inner_text
    end
  end
 
  x.report('nokogiri:xpath') do
    N.times do
      doc = Nokogiri::XML(content)
      info = doc.xpath('//location/info').first.inner_text
      url = doc.xpath('//location/refUrl').first.inner_text
    end
  end
end
 
Benchmark.bm do |x|
  x.report('hpricot:css') do
    N.times do
      doc = Hpricot.XML(content)
      info = doc.search('location info').first.inner_text
      url = doc.search('location refUrl').first.inner_text
    end
  end
 
  x.report('nokogiri:css') do
    N.times do
      doc = Nokogiri::XML(content)
      info = doc.search('location info').first.inner_text
      url = doc.search('location refUrl').first.inner_text
    end
  end
end
Result #
1
2
3
4
5
6
7
8
9
10
$ ./hpricot_vs_nokogiri.rb
      user system total real
hpricot:doc 0.230000 0.010000 0.240000 ( 0.265904)
nokogiri:doc 0.030000 0.010000 0.040000 ( 0.041641)
      user system total real
hpricot:xpath 1.050000 0.020000 1.070000 ( 1.114454)
nokogiri:xpath 0.210000 0.010000 0.220000 ( 0.226418)
      user system total real
hpricot:css 1.140000 0.030000 1.170000 ( 1.339635)
nokogiri:css 0.700000 0.010000 0.710000 ( 0.835559)
Summary #
1
2
3
Use Nokogiri's XPath selectors for fastest speed - CSS-based search is faster than Hpricot but not as fast.
 
Also take note that this benchmark is only shows parsing of XML (not HTML).