General #38
eeshannarula29
started this conversation in
General
General
#38
Replies: 1 comment
-
Small benchmarks between built-in import timeit
import random
from structlinks.DataStructures.DoublyLinkedList.DoublyLinkedList import DoublyLinkedList
# Constants
SIZE = 10_000
TARGET_INDEX = SIZE // 2
TARGET_ITEM = (TARGET_INDEX, TARGET_INDEX)
NEW_ITEM = (9999, 9999)
# Generate test data
data = [(i, i) for i in range(SIZE)]
# Setup functions
def setup_list():
return list(data)
def setup_dll():
return DoublyLinkedList(data)
def benchmark_index_list():
lst = setup_list()
return lst.index(TARGET_ITEM)
def benchmark_index_dll():
dll = setup_dll()
return dll.index(TARGET_ITEM)
def benchmark_pop_list():
lst = setup_list()
lst.pop(TARGET_INDEX)
def benchmark_pop_dll():
dll = setup_dll()
dll.pop(TARGET_INDEX)
def benchmark_pop_first_list():
lst = setup_list()
lst.pop(0)
def benchmark_pop_first_dll():
dll = setup_dll()
dll.pop(0)
def benchmark_pop_last_list():
lst = setup_list()
lst.pop()
def benchmark_pop_last_dll():
dll = setup_dll()
dll.pop(len(dll) - 1)
def benchmark_insert_list():
lst = setup_list()
lst.insert(TARGET_INDEX, NEW_ITEM)
def benchmark_insert_dll():
dll = setup_dll()
dll.insert(TARGET_INDEX, NEW_ITEM)
def benchmark_iterate_list():
lst = setup_list()
for i, item in enumerate(lst):
pass
def benchmark_iterate_dll():
dll = setup_dll()
for i, item in enumerate(dll):
pass
def run_benchmark(name, func, number=10):
time = timeit.timeit(func, number=number)
print(f"{name}: {time:.6f} seconds")
if __name__ == "__main__":
benchmarks = [
("Index - List", benchmark_index_list),
("Index - DLL", benchmark_index_dll),
("Pop - List", benchmark_pop_list),
("Pop - DLL", benchmark_pop_dll),
("Pop First - List", benchmark_pop_first_list),
("Pop First - DLL", benchmark_pop_first_dll),
("Pop Last - List", benchmark_pop_last_list),
("Pop Last - DLL", benchmark_pop_last_dll),
("Insert - List", benchmark_insert_list),
("Insert - DLL", benchmark_insert_dll),
("Iterate - List", benchmark_iterate_list),
("Iterate - DLL", benchmark_iterate_dll),
]
for name, func in benchmarks:
run_benchmark(name, func) Result:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
👋 Welcome!
We’re using Discussions as a place to connect with other members of our community. We hope that you:
build together 💪.
To get started, comment below an introduction of yourself.
Beta Was this translation helpful? Give feedback.
All reactions