Pistos (owner)

Fork Of

Revisions

gist: 21522 Download_button fork
public
Public Clone URL: git://gist.github.com/21522.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
#!/usr/bin/env ruby
require 'rubygems'
require 'open-uri'
require 'hpricot'
require 'nokogiri'
require 'better-benchmark'
 
uri = URI.parse( 'http://static.bezurk.com/fragments/wikitravel/sin.xml' )
content = uri.read
 
puts "\nhpricot vs. nokogiri: Parsing XML"
result = Benchmark.compare_realtime(
  :iterations => 10,
  :inner_iterations => 15000,
  :verbose => true
) {
  Hpricot.XML content
}.with {
  Nokogiri::XML content
}
Benchmark.report_on result
 
puts "\nhpricot vs. nokogiri: Searching with XPath"
result = Benchmark.compare_realtime(
  :iterations => 10,
  :inner_iterations => 2000,
  :verbose => true
) {
  doc = Hpricot.XML(content)
  info = doc.search('//location/info').first.inner_text
  url = doc.search('//location/refUrl').first.inner_text
}.with {
  doc = Nokogiri::XML(content)
  info = doc.xpath('//location/info').first.inner_text
  url = doc.xpath('//location/refUrl').first.inner_text
}
Benchmark.report_on result
 
puts "\nhpricot vs. nokogiri: Searching with CSS"
result = Benchmark.compare_realtime(
  :iterations => 10,
  :inner_iterations => 2000,
  :verbose => true
) {
  doc = Hpricot.XML(content)
  info = doc.search('location info').first.inner_text
  url = doc.search('location refUrl').first.inner_text
}.with {
  doc = Nokogiri::XML(content)
  info = doc.search('location info').first.inner_text
  url = doc.search('location refUrl').first.inner_text
}
Benchmark.report_on result
 
Result
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
hpricot vs. nokogiri: Parsing XML
..........
Set 1 mean: 5.368 s
Set 1 std dev: 0.052
Set 2 mean: 0.703 s
Set 2 std dev: 0.054
p.value: 1.0825088224469e-05
W: 100.0
The difference (-86.9%) IS statistically significant.
 
hpricot vs. nokogiri: Searching with XPath
..........
Set 1 mean: 3.590 s
Set 1 std dev: 0.250
Set 2 mean: 0.676 s
Set 2 std dev: 0.055
p.value: 1.0825088224469e-05
W: 100.0
The difference (-81.2%) IS statistically significant.
 
hpricot vs. nokogiri: Searching with CSS
..........
Set 1 mean: 4.237 s
Set 1 std dev: 0.061
Set 2 mean: 2.463 s
Set 2 std dev: 0.069
p.value: 1.0825088224469e-05
W: 100.0
The difference (-41.9%) IS statistically significant.
 
Summary
1
2
3
4
5
6
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).
 
This benchmark takes the original and uses better-benchmark instead.