gist: 21936 Download_button fork
public
Public Clone URL: git://gist.github.com/21936.git
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env ruby
require 'rubygems'
gem 'hpricot', '>=0.6.170'
require 'open-uri'
require 'hpricot'
require 'nokogiri'
require 'benchmark'
 
content = File.read("http://railstips.org/assets/2008/8/9/timeline.xml")
 
N = 100
 
hdoc = Hpricot.XML(content)
ndoc = Nokogiri.Hpricot(content)
hdoc2 = Hpricot.scan(content)
 
Benchmark.bm do |x|
  x.report('hpricot:doc') do
    N.times do
      Hpricot.XML(content)
    end
  end
 
  x.report('hpricot2:doc') do
    N.times do
      Hpricot.scan(content)
    end
  end
 
  x.report('nokogiri:doc') do
    N.times do
      Nokogiri.Hpricot(content)
    end
  end
end
 
Benchmark.bm do |x|
  x.report('hpricot:xpath') do
    N.times do
      info = hdoc.search('//status/text').first.inner_text
      url = hdoc.search('//user/name').first.inner_text
    end
  end
 
  x.report('hpricot2:xpath') do
    N.times do
      info = hdoc2.search('//status/text').first.inner_text
      url = hdoc2.search('//user/name').first.inner_text
    end
  end
 
  x.report('nokogiri:xpath') do
    N.times do
      info = ndoc.xpath('//status/text').first.inner_text
      url = ndoc.xpath('//user/name').first.inner_text
    end
  end
end
 
Benchmark.bm do |x|
  x.report('hpricot:css') do
    N.times do
      info = hdoc.search('status text').first.inner_text
      url = hdoc.search('user name').first.inner_text
    end
  end
 
  x.report('hpricot2:css') do
    N.times do
      info = hdoc2.search('status text').first.inner_text
      url = hdoc2.search('user name').first.inner_text
    end
  end
 
  x.report('nokogiri:css') do
    N.times do
      info = ndoc.search('status text').first.inner_text
      url = ndoc.search('user name').first.inner_text
    end
  end
end
 
Result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
      user system total real
hpricot:doc 2.630000 0.030000 2.660000 ( 2.655527)
hpricot2:doc 0.340000 0.000000 0.340000 ( 0.349340)
nokogiri:doc 0.600000 0.020000 0.620000 ( 0.611570)
      user system total real
hpricot:xpath 1.910000 0.000000 1.910000 ( 1.911496)
hpricot2:xpath 0.890000 0.010000 0.900000 ( 0.897664)
nokogiri:xpath 0.060000 0.000000 0.060000 ( 0.061546)
      user system total real
hpricot:css 1.880000 0.000000 1.880000 ( 1.889301)
hpricot2:css 0.680000 0.010000 0.690000 ( 0.677072)
nokogiri:cssbenchmark.rb:77: [BUG] Bus Error
ruby 1.8.6 (2007-09-24) [i686-darwin8.11.1]
 
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).

Owner

why

Fork Of

Forks

gist: 22990 by Pistos   Using better-benchmark.

Revisions