measurements

This commit is contained in:
Pavel 2026-05-06 22:50:33 +03:00
parent d1e3d2c791
commit 3b1ea1c519
2 changed files with 193 additions and 10 deletions

View File

@ -1,5 +1,5 @@
def ll_create_node(name, phone):
return {'name': name, 'phone': phone}
return {'name': name, 'phone': phone, 'next': None}
def ll_insert(head, name, phone):
if head is None:
@ -35,6 +35,9 @@ def ll_delete(head, name):
current['next'] = current['next']['next']
return head
current = current['next']
return head
def ll_list_all(head):
items = []
current = head

View File

@ -3,6 +3,9 @@ import random
import linked_list as ll
import hash_table as ht
import bst
import sys
sys.setrecursionlimit(20000)
def generate_records(n=10000):
records =[]
@ -11,15 +14,192 @@ def generate_records(n=10000):
phone = f"+7{random.randint(9000000000, 9999999999)}"
records.append((name, phone))
records_sorted = list(records)
records_sorted = list(records)
records_shuffled = list(records)
random.shuffle(records_shuffled)
records_shuffled = list(records)
random.shuffle(records_shuffled)
return records_sorted, records_shuffled
return records_sorted, records_shuffled
print("Генерация данных")
records_sorted, records_shuffled = generate_records()
print(f"Данные готовы: {len(records_sorted)} элементов.")
print(f"Пример (sorted): {records_sorted[:2]}")
print(f"Пример (shuffled): {records_shuffled[:2]}")
#SORTE
print("Sorte:")
#Linked list
print("Linked list")
#Вставка
head = None
start = time.perf_counter()
for name, phone in records_sorted:
head = ll.ll_insert(head, name, phone)
end = time.perf_counter()
print(f"Insert: {end - start:.4f} sec")
#Поиск
existing = random.sample(records_sorted, 100)
missing = [f"None_{i}" for i in range(10)]
start = time.perf_counter()
for name, _ in existing:
ll.ll_find(head, name)
for name in missing:
ll.ll_find(head, name)
end = time.perf_counter()
print(f"Find (110): {end - start:.6f} sec")
#Удаление
to_delete = random.sample(records_sorted, 50)
start = time.perf_counter()
for name, _ in to_delete:
head = ll.ll_delete(head, name)
end = time.perf_counter()
print(f"Delete (50): {end - start:.6f} sec")
#Hash table
print("Hash table")
#Вставка
table = ht.ht_create()
start = time.perf_counter()
for name, phone in records_sorted:
ht.ht_insert(table, name, phone)
end = time.perf_counter()
print(f"Insert: {end - start:.4f} sec")
#Поиск
start = time.perf_counter()
for name, _ in existing:
ht.ht_find(table, name)
for name in missing:
ht.ht_find(table, name)
end = time.perf_counter()
print(f"Find (110): {end - start:.6f} sec")
#УДаление
start = time.perf_counter()
for name, _ in to_delete:
ht.ht_delete(table, name)
end = time.perf_counter()
print(f"Delete (50): {end - start:.6f} sec")
#BST
print("BST")
#Вставка
root = None
start = time.perf_counter()
for name, phone in records_sorted:
root = bst.bst_insert(root, name, phone)
end = time.perf_counter()
print(f"Insert: {end - start:.4f} sec")
#Поиск
start = time.perf_counter()
for name, _ in existing:
bst.bst_find(root, name)
for name in missing:
bst.bst_find(root, name)
end = time.perf_counter()
print(f"Find (110): {end - start:.6f} sec")
#Удаление
start = time.perf_counter()
for name, _ in to_delete:
root = bst.bst_delete(root, name)
end = time.perf_counter()
print(f"Delete (50): {end - start:.6f} sec")
#SHUFFLE
print("Shuffle:")
#Linked list
print("Linked list")
#Вставка
head = None
start = time.perf_counter()
for name, phone in records_shuffled:
head = ll.ll_insert(head, name, phone)
end = time.perf_counter()
print(f"Insert: {end - start:.4f} sec")
#Поиск
existing = random.sample(records_shuffled, 100)
missing = [f"None_{i}" for i in range(10)]
start = time.perf_counter()
for name, _ in existing:
ll.ll_find(head, name)
for name in missing:
ll.ll_find(head, name)
end = time.perf_counter()
print(f"Find (110): {end - start:.6f} sec")
#Удаление
to_delete = random.sample(records_shuffled, 50)
start = time.perf_counter()
for name, _ in to_delete:
head = ll.ll_delete(head, name)
end = time.perf_counter()
print(f"Delete (50): {end - start:.6f} sec")
#Hash table
print("Hash table")
#Вставка
table = ht.ht_create()
start = time.perf_counter()
for name, phone in records_shuffled:
ht.ht_insert(table, name, phone)
end = time.perf_counter()
print(f"Insert: {end - start:.4f} sec")
#Поиск
start = time.perf_counter()
for name, _ in existing:
ht.ht_find(table, name)
for name in missing:
ht.ht_find(table, name)
end = time.perf_counter()
print(f"Find (110): {end - start:.6f} sec")
#УДаление
start = time.perf_counter()
for name, _ in to_delete:
ht.ht_delete(table, name)
end = time.perf_counter()
print(f"Delete (50): {end - start:.6f} sec")
#BST
print("BST")
#Вставка
root = None
start = time.perf_counter()
for name, phone in records_shuffled:
root = bst.bst_insert(root, name, phone)
end = time.perf_counter()
print(f"Insert: {end - start:.4f} sec")
#Поиск
start = time.perf_counter()
for name, _ in existing:
bst.bst_find(root, name)
for name in missing:
bst.bst_find(root, name)
end = time.perf_counter()
print(f"Find (110): {end - start:.6f} sec")
#Удаление
start = time.perf_counter()
for name, _ in to_delete:
root = bst.bst_delete(root, name)
end = time.perf_counter()
print(f"Delete (50): {end - start:.6f} sec")