2013-11-29 16:19:13 -08:00
|
|
|
# The Computer Language Benchmarks Game
|
|
|
|
|
# http://shootout.alioth.debian.org/
|
|
|
|
|
#
|
|
|
|
|
# contributed by Antoine Pitrou
|
|
|
|
|
# modified by Dominique Wahli
|
|
|
|
|
# modified by Heinrich Acker
|
2014-02-12 17:22:42 -08:00
|
|
|
from __future__ import print_function
|
2013-11-29 16:19:13 -08:00
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
2014-02-12 17:22:42 -08:00
|
|
|
# Map "range" to an efficient range in both Python 2 and 3.
|
|
|
|
|
try:
|
|
|
|
|
range = xrange
|
|
|
|
|
except NameError:
|
|
|
|
|
pass
|
|
|
|
|
|
2013-11-29 16:19:13 -08:00
|
|
|
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)
|
|
|
|
|
|
2014-02-12 17:22:42 -08:00
|
|
|
def check_tree(node):
|
|
|
|
|
item, left, right = node
|
2013-11-29 16:19:13 -08:00
|
|
|
if not left: return item
|
|
|
|
|
return item + check_tree(left) - check_tree(right)
|
|
|
|
|
|
|
|
|
|
min_depth = 4
|
2013-12-12 16:59:57 -08:00
|
|
|
max_depth = 12
|
2013-11-29 16:19:13 -08:00
|
|
|
stretch_depth = max_depth + 1
|
|
|
|
|
|
2021-01-31 05:40:20 +00:00
|
|
|
start = time.process_time()
|
2014-02-12 17:22:42 -08:00
|
|
|
print("stretch tree of depth %d check:" % stretch_depth, check_tree(make_tree(0, stretch_depth)))
|
2013-11-29 16:19:13 -08:00
|
|
|
|
|
|
|
|
long_lived_tree = make_tree(0, max_depth)
|
|
|
|
|
|
2014-02-12 17:22:42 -08:00
|
|
|
iterations = 2 ** max_depth
|
|
|
|
|
for depth in range(min_depth, stretch_depth, 2):
|
2013-11-29 16:19:13 -08:00
|
|
|
|
|
|
|
|
check = 0
|
2014-02-12 17:22:42 -08:00
|
|
|
for i in range(1, iterations + 1):
|
2013-11-29 16:19:13 -08:00
|
|
|
check += check_tree(make_tree(i, depth)) + check_tree(make_tree(-i, depth))
|
|
|
|
|
|
2014-02-12 17:22:42 -08:00
|
|
|
print("%d trees of depth %d check:" % (iterations * 2, depth), check)
|
|
|
|
|
iterations //= 4
|
2013-11-29 16:19:13 -08:00
|
|
|
|
2014-02-12 17:22:42 -08:00
|
|
|
print("long lived tree of depth %d check:" % max_depth, check_tree(long_lived_tree))
|
2021-01-31 05:40:20 +00:00
|
|
|
print("elapsed: " + str(time.process_time() - start))
|