Created
April 3, 2023 07:39
-
-
Save ykshatroff/161bdc3403d6031ac8e06239bc7d7a27 to your computer and use it in GitHub Desktop.
Measure removal of an element from list with different methods.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Measure removal of an element from list with different methods. | |
""" | |
import dataclasses | |
import time | |
@dataclasses.dataclass | |
class Item: | |
item_id: str | |
def remove(items: list[Item], item_id: str): | |
removed = [b for b in items if b.item_id == item_id] | |
for binding in removed: | |
items.remove(binding) | |
return bool(removed) | |
def pop(items: list[Item], item_id: str): | |
removed = False | |
for x in range(len(items) - 1, -1, -1): | |
if items[x].item_id == item_id: | |
items.pop(x) | |
removed = True | |
return bool(removed) | |
def pop_enum(items: list[Item], item_id: str): | |
d = [] | |
for x, item in enumerate(items): | |
if item.item_id == item_id: | |
d.append(x) | |
if d: | |
for x in d: | |
items.pop(x) | |
return bool(d) | |
def enum(items: list[Item], item_id: str): | |
d = False | |
for x, item in enumerate(items): | |
if item.item_id == item_id: | |
items.pop(x) | |
d = True | |
return d | |
def comp(items: list[Item], item_id: str): | |
old_len = len(items) | |
items = [b for b in items if b.item_id != item_id] | |
return len(items) < old_len | |
def comp_if_any_eq(items: list[Item], item_id: str): | |
if any(b.item_id == item_id for b in items): | |
items = [b for b in items if b.item_id != item_id] | |
return True | |
return False | |
def comp_if_any_true(items: list[Item], item_id: str): | |
if any(True for b in items if b.item_id == item_id): | |
items = [b for b in items if b.item_id != item_id] | |
return True | |
return False | |
test_cases = [ | |
# (10, [x for x in range(10)]), | |
# (50, [x for x in range(50)]), | |
(500, [x for x in range(500)]), | |
(5000, [x for x in range(5000)]), | |
] | |
if __name__ == '__main__': | |
funcs= [remove, comp, comp_if_any_eq, comp_if_any_true, pop, pop_enum, enum] | |
for x, y in test_cases: | |
print(f'--- {x} Match in the middle ---') | |
for f in funcs: | |
items = [Item(item_id=f"{i:024d}") for i in y] | |
item_id = f"{(x // 2):024d}" | |
items_copies = [list(items) for _ in range(x)] | |
start = time.perf_counter() | |
for it in range(x): | |
f(items_copies[it], item_id) | |
res = time.perf_counter() - start | |
print(f"{f.__name__}: {res:.6f}") | |
for x, y in test_cases: | |
print(f'--- {x} No match ---') | |
for f in funcs: | |
items = [Item(item_id=f"{i:024d}") for i in y] | |
item_id = f"{(x * 2):024d}" | |
items_copies = [list(items) for _ in range(x)] | |
start = time.perf_counter() | |
for it in range(x): | |
f(items_copies[it], item_id) | |
res = time.perf_counter() - start | |
print(f"{f.__name__}: {res:.6f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment