From 448558993f5dd8107643d633641ad379809e0537 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Mon, 28 Mar 2022 09:47:13 +1100 Subject: [PATCH] Add some extra benchmarks --- bench_comprehensions.py | 15 +++++++++++++++ bench_imports.py | 4 ++-- bench_try.py | 24 ++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 bench_comprehensions.py create mode 100644 bench_try.py diff --git a/bench_comprehensions.py b/bench_comprehensions.py new file mode 100644 index 0000000..8a5f103 --- /dev/null +++ b/bench_comprehensions.py @@ -0,0 +1,15 @@ +def filter_list_as_loop(): + result = [] + inputs = range(100_000) + for i in inputs: + if i % 2: + result.append(i) + +def filter_list_as_comprehension(): + inputs = range(100_000) + result = [i for i in inputs if i % 2] + + +__benchmarks__ = [ + (filter_list_as_loop, filter_list_as_comprehension, "Using a list comprehension to filter another list"), +] \ No newline at end of file diff --git a/bench_imports.py b/bench_imports.py index 878451b..e197aa0 100644 --- a/bench_imports.py +++ b/bench_imports.py @@ -9,6 +9,6 @@ def direct_import(): for _ in range(100_000): return exists('/') -__benchmarks__ = [ +__benchmarks__ = [ (dotted_import, direct_import, "Importing specific name instead of namespace"), - ] \ No newline at end of file +] \ No newline at end of file diff --git a/bench_try.py b/bench_try.py new file mode 100644 index 0000000..0ba6406 --- /dev/null +++ b/bench_try.py @@ -0,0 +1,24 @@ +def try_in_loop(): + items = { + 'a': 1 + } + for _ in range(100_000): + try: + _ = items['a'] + except Exception: + pass + +def try_outside_loop(): + items = { + 'a': 1 + } + try: + for _ in range(100_000): + _ = items['a'] + except Exception: + pass + + +__benchmarks__ = [ + (try_in_loop, try_outside_loop, "Refactoring Try..except outside a loop"), +] \ No newline at end of file