From 92971d1cfaac54ff1e793a615b416055e3f01e92 Mon Sep 17 00:00:00 2001 From: Bob Nystrom Date: Wed, 12 Feb 2014 17:22:42 -0800 Subject: [PATCH] Add python3 to benchmarks. --- benchmark/binary_trees.py | 24 ++++++++++++++++-------- benchmark/delta_blue.py | 38 ++++++++++++++++++-------------------- benchmark/fib.py | 2 ++ benchmark/for.py | 10 +++++++++- benchmark/method_call.py | 19 +++++++++++++------ benchmark/run_bench | 1 + 6 files changed, 59 insertions(+), 35 deletions(-) diff --git a/benchmark/binary_trees.py b/benchmark/binary_trees.py index 16edf1de..bab41c91 100644 --- a/benchmark/binary_trees.py +++ b/benchmark/binary_trees.py @@ -4,16 +4,24 @@ # contributed by Antoine Pitrou # modified by Dominique Wahli # modified by Heinrich Acker +from __future__ import print_function import time +# Map "range" to an efficient range in both Python 2 and 3. +try: + range = xrange +except NameError: + pass + def make_tree(item, depth): if not depth: return item, None, None item2 = item + item depth -= 1 return item, make_tree(item2 - 1, depth), make_tree(item2, depth) -def check_tree((item, left, right)): +def check_tree(node): + item, left, right = node if not left: return item return item + check_tree(left) - check_tree(right) @@ -22,19 +30,19 @@ max_depth = 12 stretch_depth = max_depth + 1 start = time.clock() -print "stretch tree of depth %d check:" % stretch_depth, check_tree(make_tree(0, stretch_depth)) +print("stretch tree of depth %d check:" % stretch_depth, check_tree(make_tree(0, stretch_depth))) long_lived_tree = make_tree(0, max_depth) -iterations = 2**max_depth -for depth in xrange(min_depth, stretch_depth, 2): +iterations = 2 ** max_depth +for depth in range(min_depth, stretch_depth, 2): check = 0 - for i in xrange(1, iterations + 1): + for i in range(1, iterations + 1): check += check_tree(make_tree(i, depth)) + check_tree(make_tree(-i, depth)) - print "%d trees of depth %d check:" % (iterations * 2, depth), check - iterations /= 4 + print("%d trees of depth %d check:" % (iterations * 2, depth), check) + iterations //= 4 -print "long lived tree of depth %d check:" % max_depth, check_tree(long_lived_tree) +print("long lived tree of depth %d check:" % max_depth, check_tree(long_lived_tree)) print("elapsed: " + str(time.clock() - start)) \ No newline at end of file diff --git a/benchmark/delta_blue.py b/benchmark/delta_blue.py index f8890f57..e973de19 100644 --- a/benchmark/delta_blue.py +++ b/benchmark/delta_blue.py @@ -18,6 +18,10 @@ the layout & logic from the original. (Ugh.) From: https://gist.github.com/toastdriven/6408132 +I (Bob Nystrom) tweaked it a bit more. It now prints some output just to be +sure it's doing the same work, and I use normal lists instead of wrapping it in +OrderedCollection. + """ from __future__ import print_function import time @@ -26,12 +30,6 @@ __author__ = 'Daniel Lindsley' __license__ = 'BSD' -# The JS variant implements "OrderedCollection", which basically completely -# overlaps with ``list``. So we'll cheat. :D -class OrderedCollection(list): - pass - - class Strength(object): REQUIRED = None STRONG_PREFERRED = None @@ -348,7 +346,7 @@ class Variable(object): super(Variable, self).__init__() self.name = name self.value = initial_value - self.constraints = OrderedCollection() + self.constraints = [] self.determined_by = None self.mark = 0 self.walk_strength = Strength.WEAKEST @@ -421,7 +419,7 @@ class Planner(object): return plan def extract_plan_from_constraints(self, constraints): - sources = OrderedCollection() + sources = [] for c in constraints: if c.is_input() and c.is_satisfied(): @@ -430,7 +428,7 @@ class Planner(object): return self.make_plan(sources) def add_propagate(self, c, mark): - todo = OrderedCollection() + todo = [] todo.append(c) while len(todo): @@ -449,8 +447,8 @@ class Planner(object): out.determined_by = None out.walk_strength = Strength.WEAKEST out.stay = True - unsatisfied = OrderedCollection() - todo = OrderedCollection() + unsatisfied = [] + todo = [] todo.append(out) while len(todo): @@ -484,7 +482,7 @@ class Planner(object): class Plan(object): def __init__(self): super(Plan, self).__init__() - self.v = OrderedCollection() + self.v = [] def add_constraint(self, c): self.v.append(c) @@ -541,7 +539,7 @@ def chain_test(n): StayConstraint(last, Strength.STRONG_DEFAULT) edit = EditConstraint(first, Strength.PREFERRED) - edits = OrderedCollection() + edits = [] edits.append(edit) plan = planner.extract_plan_from_constraints(edits) @@ -549,7 +547,7 @@ def chain_test(n): first.value = i plan.execute() - total += last.value + total += int(last.value) if last.value != i: print("Chain test failed.") @@ -569,7 +567,7 @@ def projection_test(n): offset = Variable("offset", 1000) src, dest = None, None - dests = OrderedCollection() + dests = [] for i in range(n): src = Variable("src%s" % i, i) @@ -580,27 +578,27 @@ def projection_test(n): change(src, 17) - total += dst.value + total += int(dst.value) if dst.value != 1170: print("Projection 1 failed") change(dst, 1050) - total += src.value + total += int(src.value) if src.value != 5: print("Projection 2 failed") change(scale, 5) for i in range(n - 1): - total += dests[i].value + total += int(dests[i].value) if dests[i].value != (i * 5 + 1000): print("Projection 3 failed") change(offset, 2000) for i in range(n - 1): - total += dests[i].value + total += int(dests[i].value) if dests[i].value != (i * 5 + 2000): print("Projection 4 failed") @@ -608,7 +606,7 @@ def projection_test(n): def change(v, new_value): global planner edit = EditConstraint(v, Strength.PREFERRED) - edits = OrderedCollection() + edits = [] edits.append(edit) plan = planner.extract_plan_from_constraints(edits) diff --git a/benchmark/fib.py b/benchmark/fib.py index 8324335a..c35f98f4 100644 --- a/benchmark/fib.py +++ b/benchmark/fib.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import time def fib(n): diff --git a/benchmark/for.py b/benchmark/for.py index 4f9483ac..799d7124 100644 --- a/benchmark/for.py +++ b/benchmark/for.py @@ -1,8 +1,16 @@ +from __future__ import print_function + import time +# Map "range" to an efficient range in both Python 2 and 3. +try: + range = xrange +except NameError: + pass + start = time.clock() list = [] -for i in xrange(0, 1000000): +for i in range(0, 1000000): list.append(i) sum = 0 diff --git a/benchmark/method_call.py b/benchmark/method_call.py index ac3906e8..17af5599 100644 --- a/benchmark/method_call.py +++ b/benchmark/method_call.py @@ -1,9 +1,16 @@ #!/usr/bin/python # http://www.bagley.org/~doug/shootout/ +from __future__ import print_function import sys import time +# Map "range" to an efficient range in both Python 2 and 3. +try: + range = xrange +except NameError: + pass + class Toggle(object): def __init__(self, start_state): self.bool = start_state @@ -33,7 +40,7 @@ def main(): val = 1 toggle = Toggle(val) - for i in xrange(0,NUM): + for i in range(0,NUM): val = toggle.activate().value() val = toggle.activate().value() val = toggle.activate().value() @@ -45,13 +52,13 @@ def main(): val = toggle.activate().value() val = toggle.activate().value() if val: - print "true" + print("true") else: - print "false" + print("false") val = 1 ntoggle = NthToggle(val, 3) - for i in xrange(0,NUM): + for i in range(0,NUM): val = ntoggle.activate().value() val = ntoggle.activate().value() val = ntoggle.activate().value() @@ -63,9 +70,9 @@ def main(): val = ntoggle.activate().value() val = ntoggle.activate().value() if val: - print "true" + print("true") else: - print "false" + print("false") print("elapsed: " + str(time.clock() - start)) diff --git a/benchmark/run_bench b/benchmark/run_bench index 6c1dcb3d..b3e65afb 100755 --- a/benchmark/run_bench +++ b/benchmark/run_bench @@ -47,6 +47,7 @@ LANGUAGES = [ ("lua", ["lua"], ".lua"), ("luajit (-joff)", ["luajit", "-joff"], ".lua"), ("python", ["python"], ".py"), + ("python3", ["python3"], ".py"), ("ruby", ["ruby"], ".rb") ]