Add some extra benchmarks

This commit is contained in:
Anthony Shaw
2022-03-28 09:47:13 +11:00
parent 27a494347e
commit 448558993f
3 changed files with 41 additions and 2 deletions

15
bench_comprehensions.py Normal file
View File

@@ -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"),
]

View File

@@ -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"),
]
]

24
bench_try.py Normal file
View File

@@ -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"),
]