diff options
author | scoder <none@none> | 2007-04-20 09:54:42 +0200 |
---|---|---|
committer | scoder <none@none> | 2007-04-20 09:54:42 +0200 |
commit | c101297a2fe5fbd5364628b695956f9e60d7b6da (patch) | |
tree | b2d453f039f835b9ebdebd80ab76d77c92c0ab6e /benchmark | |
parent | 7050e403512bd11ca495f5516c32b5f08bf27f1d (diff) | |
download | python-lxml-c101297a2fe5fbd5364628b695956f9e60d7b6da.tar.gz |
[svn r2230] benchmark fix: remove child iteration overhead from benchmark loops to restrict timings to the benchmark target
--HG--
branch : trunk
Diffstat (limited to 'benchmark')
-rw-r--r-- | benchmark/bench_etree.py | 116 | ||||
-rw-r--r-- | benchmark/benchbase.py | 34 |
2 files changed, 93 insertions, 57 deletions
diff --git a/benchmark/bench_etree.py b/benchmark/bench_etree.py index bbee5a00..9683f4aa 100644 --- a/benchmark/bench_etree.py +++ b/benchmark/bench_etree.py @@ -3,7 +3,7 @@ from itertools import * from StringIO import StringIO import benchbase -from benchbase import with_attributes, with_text, onlylib, serialized +from benchbase import with_attributes, with_text, onlylib, serialized, children ############################################################ # Benchmarks @@ -77,8 +77,10 @@ class BenchMark(benchbase.BenchMarkBase): root1.append(el) def bench_insert_from_document(self, root1, root2): + pos = len(root1)/2 for el in root2: - root1.insert(len(root1)/2, el) + root1.insert(pos, el) + pos = pos + 1 def bench_rotate_children(self, root): # == "1 2 3" # runs on any single tree independently @@ -102,18 +104,21 @@ class BenchMark(benchbase.BenchMarkBase): def bench_clear(self, root): root.clear() - def bench_has_children(self, root): - for child in root: + @children + def bench_has_children(self, children): + for child in children: if child and child and child and child and child: pass - def bench_len(self, root): - for child in root: + @children + def bench_len(self, children): + for child in children: map(len, repeat(child, 20)) - def bench_create_subelements(self, root): + @children + def bench_create_subelements(self, children): SubElement = self.etree.SubElement - for child in root: + for child in children: SubElement(child, '{test}test') def bench_append_elements(self, root): @@ -122,103 +127,120 @@ class BenchMark(benchbase.BenchMarkBase): el = Element('{test}test') child.append(el) - def bench_makeelement(self, root): + @children + def bench_makeelement(self, children): empty_attrib = {} - for child in root: + for child in children: child.makeelement('{test}test', empty_attrib) - def bench_create_elements(self, root): + @children + def bench_create_elements(self, children): Element = self.etree.Element - for child in root: + for child in children: Element('{test}test') - def bench_replace_children_element(self, root): + @children + def bench_replace_children_element(self, children): Element = self.etree.Element - for child in root: + for child in children: el = Element('{test}test') child[:] = [el] - def bench_replace_children(self, root): - Element = self.etree.Element - for child in root: - child[:] = [ child[0] ] + @children + def bench_replace_children(self, children): + els = [ self.etree.Element("newchild") ] + for child in children: + child[:] = els def bench_remove_children(self, root): for child in root: root.remove(child) def bench_remove_children_reversed(self, root): - for child in reversed(root[:]): + for child in reversed(root): root.remove(child) - def bench_set_attributes(self, root): - for child in root: + @children + def bench_set_attributes(self, children): + for child in children: child.set('a', 'bla') @with_attributes(True) - def bench_get_attributes(self, root): - for child in root: + @children + def bench_get_attributes(self, children): + for child in children: child.get('bla1') child.get('{attr}test1') - def bench_setget_attributes(self, root): - for child in root: + @children + def bench_setget_attributes(self, children): + for child in children: child.set('a', 'bla') - for child in root: + for child in children: child.get('a') def bench_root_getchildren(self, root): root.getchildren() - def bench_getchildren(self, root): - for child in root: + @children + def bench_getchildren(self, children): + for child in children: child.getchildren() - def bench_get_children_slice(self, root): - for child in root: + @children + def bench_get_children_slice(self, children): + for child in children: child[:] - def bench_get_children_slice_2x(self, root): - for child in root: - children = child[:] + @children + def bench_get_children_slice_2x(self, children): + for child in children: + child[:] child[:] - def bench_deepcopy(self, root): - for child in root: + @children + def bench_deepcopy(self, children): + for child in children: copy.deepcopy(child) def bench_deepcopy_all(self, root): copy.deepcopy(root) - def bench_tag(self, root): - for child in root: + @children + def bench_tag(self, children): + for child in children: child.tag - def bench_tag_repeat(self, root): - for child in root: + @children + def bench_tag_repeat(self, children): + for child in children: for i in repeat(0, 100): child.tag @with_text(utext=True, text=True, no_text=True) - def bench_text(self, root): - for child in root: + @children + def bench_text(self, children): + for child in children: child.text @with_text(utext=True, text=True, no_text=True) - def bench_text_repeat(self, root): + @children + def bench_text_repeat(self, children): repeat = range(500) - for child in root: + for child in children: for i in repeat: child.text - def bench_set_text(self, root): + @children + def bench_set_text(self, children): text = TEXT - for child in root: + for child in children: child.text = text - def bench_set_utext(self, root): + @children + def bench_set_utext(self, children): text = UTEXT - for child in root: + for child in children: child.text = text @onlylib('lxe') diff --git a/benchmark/benchbase.py b/benchmark/benchbase.py index aa197f3b..154a79e4 100644 --- a/benchmark/benchbase.py +++ b/benchmark/benchbase.py @@ -78,6 +78,11 @@ def serialized(function): function.STRING = True return function +def children(function): + "Decorator for benchmarks that require a list of root children" + function.CHILDREN = True + return function + ############################################################ # benchmark baseclass ############################################################ @@ -105,13 +110,18 @@ class BenchMarkBase(object): deepcopy = copy.deepcopy def set_property(root, fname): xml = self._serialize_tree(root) - setattr(self, fname, lambda : etree.XML(xml, etree_parser)) + if etree_parser is not None: + setattr(self, fname, lambda : etree.XML(xml, etree_parser)) + else: + setattr(self, fname, lambda : deepcopy(root)) setattr(self, fname + '_xml', lambda : xml) + setattr(self, fname + '_children', lambda : root[:]) else: def set_property(root, fname): setattr(self, fname, self.et_make_clone_factory(root)) xml = self._serialize_tree(root) setattr(self, fname + '_xml', lambda : xml) + setattr(self, fname + '_children', lambda : root[:]) attribute_list = list(izip(count(), ({}, _ATTRIBUTES))) text_list = list(izip(count(), (None, _TEXT, _UTEXT))) @@ -131,10 +141,12 @@ class BenchMarkBase(object): def _tree_builder_name(self, tree, tn, an): return '_root%d_T%d_A%d' % (tree, tn, an) - def tree_builder(self, tree, tn, an, serial): + def tree_builder(self, tree, tn, an, serial, children): name = self._tree_builder_name(tree, tn, an) if serial: name += '_xml' + elif children: + name += '_children' return getattr(self, name) def _serialize_tree(self, root): @@ -270,13 +282,14 @@ class BenchMarkBase(object): arg_count = 1 tree_tuples = self._permutations(all_trees, arg_count) - serialized = getattr(method, 'STRING', False) + serialized = getattr(method, 'STRING', False) + children = getattr(method, 'CHILDREN', False) for tree_tuple in tree_tuples: for tn in sorted(getattr(method, 'TEXT', (0,))): for an in sorted(getattr(method, 'ATTRIBUTES', (0,))): benchmarks.append((name, method_call, tree_tuple, - tn, an, serialized)) + tn, an, serialized, children)) return benchmarks @@ -315,11 +328,12 @@ def buildSuites(benchmark_class, etrees, selected): return (benchmark_suites, benchmarks) -def build_treeset_name(trees, tn, an, serialized): +def build_treeset_name(trees, tn, an, serialized, children): text = {0:'-', 1:'S', 2:'U'}[tn] attr = {0:'-', 1:'A'}[an] ser = {True:'X', False:'T'}[serialized] - return "%s%s%s T%s" % (text, attr, ser, ',T'.join(imap(str, trees))[:6]) + chd = {True:'C', False:'R'}[children] + return "%s%s%s%s T%s" % (text, attr, ser, chd, ',T'.join(imap(str, trees))[:6]) def printSetupTimes(benchmark_suites): print "Setup times for trees in seconds:" @@ -327,20 +341,20 @@ def printSetupTimes(benchmark_suites): print "%-3s: " % b.lib_name, for an in (0,1): for tn in (0,1,2): - print ' %s ' % build_treeset_name((), tn, an, False)[:2], + print ' %s ' % build_treeset_name((), tn, an, False, False)[:2], print for i, tree_times in enumerate(b.setup_times): print " T%d:" % (i+1), ' '.join("%6.4f" % t for t in tree_times) print -def runBench(suite, method_name, method_call, tree_set, tn, an, serial): +def runBench(suite, method_name, method_call, tree_set, tn, an, serial, children): if method_call is None: raise SkippedTest current_time = time.time call_repeat = range(10) - tree_builders = [ suite.tree_builder(tree, tn, an, serial) + tree_builders = [ suite.tree_builder(tree, tn, an, serial, children) for tree in tree_set ] times = [] @@ -364,7 +378,7 @@ def runBenchmarks(benchmark_suites, benchmarks): for bench_calls in izip(*benchmarks): for lib, (bench, benchmark_setup) in enumerate(izip(benchmark_suites, bench_calls)): bench_name = benchmark_setup[0] - tree_set_name = build_treeset_name(*benchmark_setup[-4:]) + tree_set_name = build_treeset_name(*benchmark_setup[-5:]) print "%-3s: %-28s" % (bench.lib_name, bench_name[6:34]), print "(%-10s)" % tree_set_name, sys.stdout.flush() |