| 1 |
-- TODO: build some generic benchmark harness |
| 2 |
[case testBenchmarkTree] |
| 3 |
from typing import Optional |
| 4 |
class Node: |
| 5 |
def __init__(self, value: int) -> None: |
| 6 |
self.value = value |
| 7 |
self.left = None # type: Optional[Node] |
| 8 |
self.right = None # type: Optional[Node] |
| 9 |
def sum(self) -> int: |
| 10 |
left = 0 |
| 11 |
if self.left is not None: |
| 12 |
left = self.left.sum() |
| 13 |
right = 0 |
| 14 |
if self.right is not None: |
| 15 |
right = self.right.sum() |
| 16 |
return self.value + left + right |
| 17 |
def sum_tree(x: Optional[Node]) -> int: |
| 18 |
if x is None: |
| 19 |
return 0 |
| 20 |
return x.value + sum_tree(x.left) + sum_tree(x.right) |
| 21 |
def build(n: int) -> Optional[Node]: |
| 22 |
if n == 0: |
| 23 |
return None |
| 24 |
x = Node(n) |
| 25 |
x.left = build(n - 1) |
| 26 |
x.right = x.left |
| 27 |
return x |
| 28 |
|
| 29 |
def bench_sum(x: Optional[Node]) -> None: |
| 30 |
for i in range(1000000): |
| 31 |
sum_tree(x) |
| 32 |
def bench_sum_method(x: Node) -> None: |
| 33 |
for i in range(1000000): |
| 34 |
x.sum() |
| 35 |
[file driver.py] |
| 36 |
from typing import Optional |
| 37 |
import native |
| 38 |
import interpreted |
| 39 |
from timeit import timeit |
| 40 |
from time import time |
| 41 |
import os |
| 42 |
|
| 43 |
def dumb_time(f): |
| 44 |
t0 = time() |
| 45 |
f() |
| 46 |
t1 = time() |
| 47 |
return t1 - t0 |
| 48 |
|
| 49 |
def basic_test(m): |
| 50 |
tree = m.build(5) |
| 51 |
assert(m.sum_tree(tree) == 57) |
| 52 |
assert(tree.sum() == 57) |
| 53 |
return tree |
| 54 |
|
| 55 |
def test(m): |
| 56 |
tree = basic_test(m) |
| 57 |
|
| 58 |
g = {**globals(), **locals()} |
| 59 |
sum = timeit('m.sum_tree(tree)', globals=g) |
| 60 |
sum2 = timeit('tree.sum()', globals=g) |
| 61 |
fsum = dumb_time(lambda: m.bench_sum(tree)) |
| 62 |
fsum2 = dumb_time(lambda: m.bench_sum_method(tree)) |
| 63 |
build = timeit('m.build(5)', globals=g) |
| 64 |
return (sum, sum2, fsum, fsum2, build) |
| 65 |
|
| 66 |
# Basic functionality test |
| 67 |
basic_test(native) |
| 68 |
|
| 69 |
# Benchmark if we are benchmarking |
| 70 |
if os.environ.get('MYPYC_RUN_BENCH') == '1': |
| 71 |
nsum, nsum2, nfsum, nfsum2, nbuild = test(native) |
| 72 |
isum, isum2, ifsum, ifsum2, ibuild = test(interpreted) |
| 73 |
print(nsum, nsum2, nfsum, nbuild) |
| 74 |
print("Sum speedup:", isum/nsum) |
| 75 |
print("Sum method speedup:", isum2/nsum2) |
| 76 |
print("Sum (fast) speedup:", ifsum/nfsum) |
| 77 |
print("Sum (fast) method speedup:", ifsum2/nfsum2) |
| 78 |
print("Build speedup:", ibuild/nbuild) |
| 79 |
|
| 80 |
[case testBenchmarkVisitorTree] |
| 81 |
from mypy_extensions import trait |
| 82 |
from typing import cast, Generic, TypeVar, Any |
| 83 |
|
| 84 |
T = TypeVar('T') |
| 85 |
class Tree: |
| 86 |
def accept(self, v: 'TreeVisitor[T]') -> T: |
| 87 |
pass |
| 88 |
class Leaf(Tree): |
| 89 |
def accept(self, v: 'TreeVisitor[T]') -> T: |
| 90 |
return v.visit_leaf(self) |
| 91 |
class Node(Tree): |
| 92 |
def __init__(self, value: int, left: Tree, right: Tree) -> None: |
| 93 |
self.value = value |
| 94 |
self.left = left |
| 95 |
self.right = right |
| 96 |
def accept(self, v: 'TreeVisitor[T]') -> T: |
| 97 |
return v.visit_node(self) |
| 98 |
|
| 99 |
@trait |
| 100 |
class TreeVisitor(Generic[T]): |
| 101 |
def visit_leaf(self, x: Leaf) -> T: return cast(T, None) |
| 102 |
def visit_node(self, x: Node) -> T: return cast(T, None) |
| 103 |
|
| 104 |
class SumVisitor(TreeVisitor[int]): |
| 105 |
def sum(self, x: Tree) -> int: |
| 106 |
return x.accept(self) |
| 107 |
def visit_leaf(self, x: Leaf) -> int: |
| 108 |
return 0 |
| 109 |
def visit_node(self, x: Node) -> int: |
| 110 |
return x.value + self.sum(x.left) + self.sum(x.right) |
| 111 |
|
| 112 |
def equal(x: Tree, y: Tree) -> bool: |
| 113 |
return EqualVisitor(x).equal(y) |
| 114 |
|
| 115 |
class EqualVisitor(TreeVisitor[bool]): |
| 116 |
def __init__(self, left: Tree) -> None: |
| 117 |
self.left = left |
| 118 |
def equal(self, right: Tree) -> bool: |
| 119 |
return right.accept(self) |
| 120 |
def visit_leaf(self, right: Leaf) -> bool: |
| 121 |
return isinstance(self.left, Leaf) |
| 122 |
def visit_node(self, right: Node) -> bool: |
| 123 |
if isinstance(self.left, Node): |
| 124 |
# our boolean stuff is crap |
| 125 |
if (self.left.value == right.value and equal(self.left.left, right.left) |
| 126 |
and equal(self.left.right, right.right)): |
| 127 |
return True |
| 128 |
return False |
| 129 |
|
| 130 |
def sum_tree(x: Tree) -> int: |
| 131 |
return SumVisitor().sum(x) |
| 132 |
|
| 133 |
def build(n: int) -> Tree: |
| 134 |
if n == 0: |
| 135 |
return Leaf() |
| 136 |
return Node(n, build(n - 1), build(n - 1)) |
| 137 |
|
| 138 |
def bench_sum_tree(x: Tree) -> None: |
| 139 |
for i in range(100000): |
| 140 |
sum_tree(x) |
| 141 |
def bench_equal_tree(x: Tree, y: Tree) -> None: |
| 142 |
for i in range(100000): |
| 143 |
equal(x, y) |
| 144 |
|
| 145 |
[file driver.py] |
| 146 |
from typing import Optional |
| 147 |
import interpreted |
| 148 |
import native |
| 149 |
from timeit import timeit |
| 150 |
from time import time |
| 151 |
import os |
| 152 |
import sys |
| 153 |
|
| 154 |
# Side test: some stuff about MROs and generics |
| 155 |
if sys.version_info[:3] > (3, 5, 2): |
| 156 |
assert tuple(x.__name__ for x in interpreted.SumVisitor.mro()) == ('SumVisitor', 'TreeVisitor', 'Generic', 'object') |
| 157 |
assert tuple(x.__name__ for x in native.SumVisitor.mro()) == ('SumVisitor', 'TreeVisitor', 'Generic', 'object') |
| 158 |
assert str(native.TreeVisitor[native.T]) == "native.TreeVisitor[~T]" |
| 159 |
|
| 160 |
assert native.TreeVisitor.__name__ == "TreeVisitor" |
| 161 |
assert native.SumVisitor.__name__ == "SumVisitor" |
| 162 |
|
| 163 |
def dumb_time(f): |
| 164 |
t0 = time() |
| 165 |
f() |
| 166 |
t1 = time() |
| 167 |
return t1 - t0 |
| 168 |
|
| 169 |
def basic_test(m): |
| 170 |
tree = m.build(5) |
| 171 |
tree2 = m.build(5) |
| 172 |
tree2.right.right.right.value = 10 |
| 173 |
assert m.sum_tree(tree) == 57 |
| 174 |
assert m.equal(tree, tree) |
| 175 |
assert not m.equal(tree, tree2) |
| 176 |
|
| 177 |
assert isinstance(native.SumVisitor(), native.TreeVisitor) |
| 178 |
|
| 179 |
return tree |
| 180 |
|
| 181 |
def test(m): |
| 182 |
tree = basic_test(m) |
| 183 |
|
| 184 |
g = {**globals(), **locals()} |
| 185 |
fsum = dumb_time(lambda: m.bench_sum_tree(tree)) |
| 186 |
feq = dumb_time(lambda: m.bench_equal_tree(tree, tree)) |
| 187 |
return fsum, feq |
| 188 |
|
| 189 |
basic_test(native) |
| 190 |
|
| 191 |
if os.environ.get('MYPYC_RUN_BENCH') == '1': |
| 192 |
nfsum, nfeq = test(native) |
| 193 |
ifsum, ifeq = test(interpreted) |
| 194 |
print(nfsum) |
| 195 |
print("Sum speedup:", ifsum/nfsum) |
| 196 |
print("Equal speedup:", ifeq/nfeq) |