diff --git a/KuznetsovYuM/docs/data/1-st-exercise/phonebook_structures.py b/KuznetsovYuM/docs/data/1-st-exercise/phonebook_structures.py index 3670595..3087a84 100644 --- a/KuznetsovYuM/docs/data/1-st-exercise/phonebook_structures.py +++ b/KuznetsovYuM/docs/data/1-st-exercise/phonebook_structures.py @@ -159,12 +159,120 @@ def bst_collect_inorder(root): return result + + +#Benchmarking +import random +import time +import csv +import os +import sys + +sys.setrecursionlimit(20000) + +def generate_test_data(n, seed=42): + random.seed(seed) + records = [] + for i in range(1, n+1): + name = f"User_{i:05d}" + phone = f"{random.randint(100,999)}-{random.randint(1000,9999)}" + records.append((name, phone)) + return records + +def prepare_ordered_and_shuffled(records): + shuffled = records.copy() + random.shuffle(shuffled) + sorted_records = sorted(records, key=lambda x: x[0]) + return shuffled, sorted_records + +def measure_operations(struct_ops, records, mode_name, repeats=5): + results = [] + for rep in range(repeats): + ds = struct_ops['create']() + + start = time.perf_counter() + for name, phone in records: + ds = struct_ops['insert'](ds, name, phone) + insert_time = time.perf_counter() - start + + existing_names = [name for name, _ in records] + sample_existing = random.sample(existing_names, 100) + nonexistent = [f"Missing_{i}" for i in range(10)] + search_names = sample_existing + nonexistent + random.shuffle(search_names) + + start = time.perf_counter() + for name in search_names: + struct_ops['find'](ds, name) + find_time = time.perf_counter() - start + + to_delete = random.sample(existing_names, 50) + start = time.perf_counter() + for name in to_delete: + ds = struct_ops['delete'](ds, name) + delete_time = time.perf_counter() - start + + results.append({ + 'structure': struct_ops['name'], + 'mode': mode_name, + 'repetition': rep+1, + 'insert_time': insert_time, + 'find_time': find_time, + 'delete_time': delete_time + }) + return results + +def run_full_benchmark(): + N = 10000 + base_records = generate_test_data(N) + shuffled, sorted_records = prepare_ordered_and_shuffled(base_records) + + structures = { + 'LinkedList': { + 'name': 'LinkedList', + 'create': lambda: None, + 'insert': linked_list_add, + 'find': linked_list_find, + 'delete': linked_list_remove, + }, + 'HashTable': { + 'name': 'HashTable', + 'create': lambda: hash_table_create(100), + 'insert': hash_table_put, + 'find': hash_table_get, + 'delete': hash_table_remove, + }, + 'BST': { + 'name': 'BST', + 'create': lambda: None, + 'insert': bst_add, + 'find': bst_find, + 'delete': bst_remove, + } + } + + all_results = [] + for name, ops in structures.items(): + print(f"Benchmarking {name} on random order...") + all_results.extend(measure_operations(ops, shuffled, 'random', repeats=5)) + print(f"Benchmarking {name} on sorted order...") + all_results.extend(measure_operations(ops, sorted_records, 'sorted', repeats=5)) + + os.makedirs('docs/data', exist_ok=True) + csv_path = 'docs/data/experiment_results.csv' + with open(csv_path, 'w', newline='', encoding='utf-8') as f: + writer = csv.writer(f) + writer.writerow(['Structure', 'Mode', 'Repeat', 'Insert (sec)', 'Search (sec)', 'Delete (sec)']) + for r in all_results: + writer.writerow([ + r['structure'], + r['mode'], + r['repetition'], + f"{r['insert_time']:.6f}", + f"{r['find_time']:.6f}", + f"{r['delete_time']:.6f}" + ]) + print(f"Experiment finished. Results saved to {csv_path}") + if __name__ == '__main__': - tree = None - tree = bst_add(tree, 'Zoe', '111') - tree = bst_add(tree, 'Alice', '222') - tree = bst_add(tree, 'Bob', '333') - tree = bst_add(tree, 'Alice', '444') - print(bst_find(tree, 'Alice')) # 444 - tree = bst_remove(tree, 'Bob') - print(bst_collect_inorder(tree)) # [('Alice','444'), ('Zoe','111')] \ No newline at end of file + run_full_benchmark() \ No newline at end of file