source: exist/trunk/python/elementtree-1.3a6-20070212-badc/benchmark.py @ 3429

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/python/elementtree-1.3a6-20070212-badc/benchmark.py@4663
Revision 3429, 2.4 KB checked in by pjkersha, 12 years ago (diff)

Added latest ElementTree version (12/2/08) from Fredrik Lundh

Line 
1# $Id: benchmark.py 3377 2008-02-13 08:17:57Z fredrik $
2# simple elementtree benchmark program
3
4from elementtree import ElementTree
5try:
6    import cElementTree
7except ImportError:
8    try:
9        from xml.etree import cElementTree
10    except ImportError:
11        cElementTree = None
12try:
13    from elementtree import XMLTreeBuilder # xmllib
14except ImportError:
15    XMLTreeBuilder = None
16try:
17    from elementtree import SimpleXMLTreeBuilder # xmllib
18except ImportError:
19    SimpleXMLTreeBuilder = None
20try:
21    from elementtree import SgmlopXMLTreeBuilder # sgmlop
22except ImportError:
23    SgmlopXMLTreeBuilder = None
24try:
25    from xml.dom import minidom # pyexpat+minidom
26except ImportError:
27    minidom = None
28
29import sys, time
30
31try:
32    file = sys.argv[1]
33except IndexError:
34    file = "hamlet.xml"
35
36def benchmark(file, builder_module):
37    source = open(file, "rb")
38    t0 = time.time()
39    parser = builder_module.TreeBuilder()
40    while 1:
41        data = source.read(32768)
42        if not data:
43            break
44        parser.feed(data)
45    tree = parser.close()
46    t1 = time.time()
47    print "%s: %d nodes read in %.3f seconds" % (
48        builder_module.__name__, len(tree.getiterator()), t1-t0
49        )
50    raw_input("press return to continue...")
51    del tree
52
53def benchmark_parse(file, driver):
54    t0 = time.time()
55    tree = driver.parse(file)
56    t1 = time.time()
57    print driver.__name__ + ".parse done in %.3f seconds" % (t1-t0)
58    raw_input("press return to continue...")
59    del tree
60
61def benchmark_minidom(file):
62    t0 = time.time()
63    dom = minidom.parse(file)
64    t1 = time.time()
65    print "minidom tree read in %.3f seconds" % (t1-t0)
66    raw_input("press return to continue...")
67    del dom
68
69# gobble gobble
70for i in range(3):
71    text = open(file).read()
72
73# baseline
74benchmark_parse(file, ElementTree)
75
76if cElementTree:
77    benchmark_parse(file, cElementTree)
78
79if sys.platform != "cli":
80    if XMLTreeBuilder:
81        benchmark(file, XMLTreeBuilder)
82    if SimpleXMLTreeBuilder:
83        benchmark(file, SimpleXMLTreeBuilder) # use xmllib
84    try:
85        if SgmlopXMLTreeBuilder:
86            benchmark(file, SgmlopXMLTreeBuilder) # use sgmlop
87    except RuntimeError, v:
88        print "=== SgmlopXMLTreeBuilder not available (%s)" % v
89
90if minidom:
91    benchmark_minidom(file)
92else:
93    print "=== minidom not available"
Note: See TracBrowser for help on using the repository browser.