From 3bc8d728952375e614387dcfce3f7bd557e4f5a3 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Thu, 2 Jun 2022 08:01:55 +0900 Subject: [PATCH] Take best-of-ten in most benchmarks, add inner_loop benchmarks --- bench/bench.krk | 2 +- bench/bench.py | 2 +- bench/inner_loop.krk | 56 ++++++++++++++++++++++++++++++++++++++++++++ bench/inner_loop.py | 48 +++++++++++++++++++++++++++++++++++++ bench/list.krk | 2 +- bench/list.py | 2 +- bench/maketree.krk | 2 +- bench/maketree.py | 2 +- 8 files changed, 110 insertions(+), 6 deletions(-) create mode 100644 bench/inner_loop.krk create mode 100644 bench/inner_loop.py 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')