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/ruby -I. -w
require "benchmark"
require "rexml/document"
include Benchmark
n = 400
bm(20) do |test|
File.open( "project.xml" ) do |source|
test.report("parsing file") {
n.times {
REXML::Document.new source
source.rewind
}
}
end
test.report("Document.new") { n.times { REXML::Document.new }}
test.report("new element") { n.times { REXML::Element.new }}
document = REXML::Document.new
test.report("add element") { n.times {
document.add_element( "tag1", {"blah" => "four"} ) }
}
el1 = REXML::Element.new
test.report("add child") { n.times {
el2 = REXML::Element.new
el1 << el2
}}
test.report("Tree manipulation") { n.times {
doc = REXML::Document.new
doc.add_element("tag1", {"blah"=>"four"}) # Attributes are optional
# The following is faster than add_element
tag2 = REXML::Element.new("tag2")
tag2.attributes["some"] = "valu"
doc.root.add_element << tag2 # Same as root.add_element
tag3 = REXML::Element.new("tag3")
tag2.elements << tag3
}}
File.open( "project.xml" ) do |source|
document = REXML::Document.new source
end
test.report("Writing tree") { n.times {
string = ""
document.write string
}}
test.report("Detecting children") { n.times {
element = document.root.detect {|element|
element.instance_of? REXML::Element and element.name == "Datasets"
}
element = element.detect{|element|
element.instance_of? REXML::Element and
element.name == "link" and
element.attributes["idref"] == '18'
}
}}
test.report("XPath searching") { n.times {
element = document.root.elements["Datasets/link[@idref='18']"]
}}
end
|