/xmlbench/trunk

To get this branch, use:
bzr branch http://darksoft.org/webbzr/xmlbench/trunk

« back to all changes in this revision

Viewing changes to parse/scripts/libxml.ruby

  • Committer: Suren A. Chilingaryan
  • Date: 2009-10-02 01:16:02 UTC
  • Revision ID: csa@dside.dyndns.org-20091002011602-ut1jl0go12npun6y
DOM walking for all Libxml bindings: ruby, python, perl, php, lisp

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
require 'xml'
4
4
 
5
 
def parse(xml)
 
5
#class Array; def sum; inject( 0 ) { |sum,x| sum+x }; end; end
 
6
 
 
7
 
 
8
def calc(node)
 
9
    res = Float(node.child.content) rescue 0 if (node.child and node.child.text?())
 
10
    return res if (res)
 
11
    node.each_attr do |attr|
 
12
        res = Float(attr.child.content) rescue 0
 
13
        return res if (res)
 
14
    end
 
15
    return 0
 
16
end
 
17
 
 
18
def siblings(node)
 
19
    res = 0
 
20
 
 
21
    i=0;
 
22
    node.children.each { |cur_node|
 
23
        break if (i+=1)>3
 
24
    }
 
25
    
 
26
    cond = if (i>3); 1 else; nil end
 
27
        
 
28
    node.children.each { |cur_node|
 
29
        if cur_node.element?
 
30
            if cond
 
31
                res += calc(cur_node)
 
32
            end
 
33
            res += siblings(cur_node)
 
34
        end
 
35
    }
 
36
    
 
37
    return res
 
38
end
 
39
 
 
40
def parse(xml, walk)
6
41
    parser = XML::Parser.file(xml)
7
42
    doc = parser.parse
8
43
#    print doc    
 
44
    if (walk)
 
45
#       This is slowest 330 ms
 
46
#       sum = siblings(doc.root)
 
47
        
 
48
        sum = 0
 
49
#       doc.find("//*[(@* or text()) and (count(../child::*)>3)]").each do |node|
 
50
 
 
51
        doc.find("//*[(number(@*) or number(text())) and (count(../child::*)>3)]").each do |node|
 
52
#           190 ms
 
53
#           sum += calc(node)
 
54
            
 
55
            #Fastest approach ~ 177ms
 
56
 
 
57
            res = Float(node.child.content) rescue 0 if (node.child and node.child.text?())
 
58
            if (res)
 
59
                sum += res
 
60
            else
 
61
                node.each_attr do |attr|
 
62
                    res = Float(attr.child.content) rescue 0
 
63
                    if (res)
 
64
                        sum += res
 
65
                        break
 
66
                    end
 
67
                end
 
68
            end
 
69
        end
 
70
 
 
71
=begin
 
72
        sum = doc.find("//*[(number(@*) or number(text())) and (count(../child::*)>3)]").map { |node|
 
73
            res = Float(node.child.content) rescue nil if (node.child and node.child.text?())
 
74
            next res if ((res)&&(res!=0))
 
75
            
 
76
            node.each_attr do |attr|
 
77
                res = Float(attr.child.content) rescue nil
 
78
                break res if ((res)&&(res!=0))
 
79
            end
 
80
        }.sum
 
81
=end
 
82
 
 
83
=begin
 
84
        sum = doc.find("//*[(number(@*) or number(text())) and (count(../child::*)>3)]").inject(0) { |sum, node|
 
85
            res = Float(node.child.content) rescue nil if (node.child and node.child.text?())
 
86
            next sum+res if ((res)&&(res!=0))
 
87
            
 
88
            node.each_attr do |attr|
 
89
                res = Float(attr.child.content) rescue nil
 
90
                break res if ((res)&&(res!=0))
 
91
            end + sum
 
92
        }
 
93
=end
 
94
 
 
95
#       print "Sum: #{sum}\n"
 
96
    end
9
97
end
10
98
 
 
99
walk_tree = ENV['walk_tree']
 
100
 
11
101
if ($*.length > 0)
12
102
    iterations = $*[0].to_i
13
103
else
20
110
    filename = lambda { |i| "../xml.tmp/#{i}.xml" }
21
111
end
22
112
 
23
 
parse(filename.call(0))
 
113
parse(filename.call(0), walk_tree)
24
114
 
25
115
iterations.times { |i|
26
 
    parse(filename.call(i+1))
 
116
    parse(filename.call(i+1), walk_tree)
27
117
}