[1] генерация и замер времени

This commit is contained in:
ivanchenkoam 2026-05-23 16:35:05 +03:00
parent 7fafaba9eb
commit 5343261de3

View File

@ -237,3 +237,93 @@ def bst_list_all(root: Optional[Dict]) -> List[Tuple[str, str]]:
records.sort(key=lambda x: x[0]) records.sort(key=lambda x: x[0])
return records return records
def generate_records(n: int) -> List[Tuple[str, str]]:
"""Генерация записей"""
records = [(f"User_{i:05d}", f"+7-999-{i:07d}") for i in range(n)]
return records
def measure_insertion(structure_type: str, data: List[Tuple[str, str]],
ht_size: int = 1000) -> float:
"""Замер времени вставки"""
start = time.perf_counter()
if structure_type == "LinkedList":
head = None
for name, phone in data:
head = ll_insert(head, name, phone)
elif structure_type == "HashTable":
buckets = ht_create(ht_size)
for name, phone in data:
ht_insert(buckets, name, phone)
elif structure_type == "BST":
root = None
for name, phone in data:
root = bst_insert(root, name, phone)
end = time.perf_counter()
return end - start
def measure_find(structure_type: str, data: List[Tuple[str, str]],
existing_names: List[str], missing_names: List[str],
ht_size: int = 1000) -> Tuple[float, Any]:
"""Замер времени поиска (возвращает время и структуру для удаления)"""
# Сначала создаём структуру
if structure_type == "LinkedList":
head = None
for name, phone in data:
head = ll_insert(head, name, phone)
start = time.perf_counter()
for name in existing_names + missing_names:
ll_find(head, name)
end = time.perf_counter()
return end - start, head
elif structure_type == "HashTable":
buckets = ht_create(ht_size)
for name, phone in data:
ht_insert(buckets, name, phone)
start = time.perf_counter()
for name in existing_names + missing_names:
ht_find(buckets, name)
end = time.perf_counter()
return end - start, buckets
elif structure_type == "BST":
root = None
for name, phone in data:
root = bst_insert(root, name, phone)
start = time.perf_counter()
for name in existing_names + missing_names:
bst_find(root, name)
end = time.perf_counter()
return end - start, root
def measure_delete(structure_type: str, structure: Any,
names_to_delete: List[str]) -> float:
"""Замер времени удаления"""
start = time.perf_counter()
if structure_type == "LinkedList":
head = structure
for name in names_to_delete:
head = ll_delete(head, name)
elif structure_type == "HashTable":
buckets = structure
for name in names_to_delete:
ht_delete(buckets, name)
elif structure_type == "BST":
root = structure
for name in names_to_delete:
root = bst_delete(root, name)
end = time.perf_counter()
return end - start