feat: add python3 benchmark runner and fix python3 compatibility in benchmark scripts

Add python3 as a new language entry in the benchmark runner configuration, enabling automated performance testing with Python 3. Update multiple benchmark scripts (binary_trees.py, delta_blue.py, fib.py, for.py, method_call.py) to be compatible with Python 3 by adding `from __future__ import print_function`, replacing `xrange` with a `range` polyfill, converting print statements to function calls, and replacing integer division with floor division. Remove the custom `OrderedCollection` class in delta_blue.py in favor of standard Python lists.
This commit is contained in:
Bob Nystrom
2014-02-13 01:22:42 +00:00
parent 05857c8b47
commit c04865df2e
6 changed files with 59 additions and 35 deletions
+16 -8
View File
@@ -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))