diff --git a/bench/bench.krk b/bench/bench.krk index 57ddcad..cdc5de3 100644 --- a/bench/bench.krk +++ b/bench/bench.krk @@ -132,5 +132,5 @@ if __name__=='__main__': read_classvar, read_instancevar, read_unboundmethod, read_boundmethod, call_boundmethod, write_local, write_nonlocal, write_global, write_classvar, write_instancevar]: - print(timeit(f,number=1000000), f.__qualname__) + print(min(timeit(f,number=100000) for x in range(10)), f.__qualname__) diff --git a/bench/bench.py b/bench/bench.py index d1e11ec..400c955 100644 --- a/bench/bench.py +++ b/bench/bench.py @@ -136,5 +136,5 @@ if __name__=='__main__': read_classvar, read_instancevar, read_unboundmethod, read_boundmethod, call_boundmethod, write_local, write_nonlocal, write_global, write_classvar, write_instancevar]: - print(timeit(f,number=1000000), f.__qualname__ if hasattr(f,'__qualname__') else f.__name__ if hasattr(f,'__name__') else '?') + print(min(timeit(f,number=100000) for x in range(10)), f.__qualname__ if hasattr(f,'__qualname__') else f.__name__ if hasattr(f,'__name__') else '?') diff --git a/bench/inner_loop.krk b/bench/inner_loop.krk new file mode 100644 index 0000000..24384b9 --- /dev/null +++ b/bench/inner_loop.krk @@ -0,0 +1,56 @@ +import timeit + +if True: + let xs = [[[x for x in range(30)] for y in range(30)] for z in range(30)] + xs[29][29][29] = 42 + + def breaks(): + let _z + for ys in xs: + for zs in ys: + for z in zs: + if z == 42: + _z = z + break + else: + continue + break + else: + continue + break + else: + return None + return _z + + class FoundIt(Exception): + pass + + def exceptions(): + let _z + try: + for ys in xs: + for zs in ys: + for z in zs: + if z == 42: + _z = z + raise FoundIt() + except FoundIt as e: + return _z + + def _inner(): + for ys in xs: + for zs in ys: + for z in zs: + if z == 42: + return z + + def functions(): + return _inner() + + import gc + gc.collect() + print(min(timeit.timeit(breaks,number=100) for x in range(10)),"breaks") + gc.collect() + print(min(timeit.timeit(functions,number=100) for x in range(10)),"functions") + gc.collect() + print(min(timeit.timeit(exceptions,number=100) for x in range(10)),"exceptions") diff --git a/bench/inner_loop.py b/bench/inner_loop.py new file mode 100644 index 0000000..f5f44f6 --- /dev/null +++ b/bench/inner_loop.py @@ -0,0 +1,48 @@ +import fasttimer as timeit + +xs = [[[x for x in range(30)] for y in range(30)] for z in range(30)] +xs[29][29][29] = 42 + +def breaks(): + for ys in xs: + for zs in ys: + for z in zs: + if z == 42: + break + else: + continue + break + else: + continue + break + else: + return None + return z + +class FoundIt(Exception): + def __init__(self, needle): + self.needle = needle + +def exceptions(): + try: + for ys in xs: + for zs in ys: + for z in zs: + if z == 42: + raise FoundIt(z) + except FoundIt as e: + return e.needle + +def _inner(): + for ys in xs: + for zs in ys: + for z in zs: + if z == 42: + return z + +def functions(): + return _inner() + +print(min(timeit.timeit(breaks,number=100) for x in range(10)),"breaks") +print(min(timeit.timeit(functions,number=100) for x in range(10)),"functions") +print(min(timeit.timeit(exceptions,number=100) for x in range(10)),"exceptions") diff --git a/bench/list.krk b/bench/list.krk index 9d83b87..1c74a26 100644 --- a/bench/list.krk +++ b/bench/list.krk @@ -8,4 +8,4 @@ def func(): add(1) pop() -print(timeit(func), "list append") +print(min(timeit(func,number=100000) for x in range(10)), "list append") diff --git a/bench/list.py b/bench/list.py index 8d497ed..244ae8d 100644 --- a/bench/list.py +++ b/bench/list.py @@ -8,4 +8,4 @@ def func(): add(1) pop() -print(timeit(func), "list append") +print(min(timeit(func,number=100000) for x in range(10)), "list append") diff --git a/bench/maketree.krk b/bench/maketree.krk index 54c2145..b1ae418 100644 --- a/bench/maketree.krk +++ b/bench/maketree.krk @@ -11,4 +11,4 @@ def makeTree(depth): if __name__ == '__main__': from timeit import timeit - print(timeit(lambda: makeTree(16), number=10), 'makeTree') + print(min(timeit(lambda: makeTree(16), number=1) for x in range(10)), 'makeTree') diff --git a/bench/maketree.py b/bench/maketree.py index 2c5ed71..8f432d5 100644 --- a/bench/maketree.py +++ b/bench/maketree.py @@ -11,4 +11,4 @@ def makeTree(depth): if __name__ == '__main__': from fasttimer import timeit - print(timeit(lambda: makeTree(16), number=10), 'makeTree') + print(min(timeit(lambda: makeTree(16), number=1) for x in range(10)), 'makeTree')