Генерация данных и функции замеров
This commit is contained in:
parent
da179c5dde
commit
27417decc0
|
|
@ -177,4 +177,107 @@ def bst_inorder_collect(root, records=None):
|
|||
records.append((root['name'], root['phone']))
|
||||
bst_inorder_collect(root['right'], records)
|
||||
return records
|
||||
def generate_records(N=10000):
|
||||
records = []
|
||||
for i in range(N):
|
||||
name = f"User_{i:05d}"
|
||||
phone = f"+7-999-{random.randint(1000000, 9999999)}"
|
||||
records.append((name, phone))
|
||||
return records
|
||||
|
||||
def measure_insertion(struct_type, records):
|
||||
times = []
|
||||
for _ in range(REPEATS):
|
||||
if struct_type == 'll':
|
||||
head = None
|
||||
start = time.perf_counter()
|
||||
for name, phone in records:
|
||||
head = ll_insert(head, name, phone)
|
||||
end = time.perf_counter()
|
||||
elif struct_type == 'ht':
|
||||
buckets = ht_create(2000)
|
||||
start = time.perf_counter()
|
||||
for name, phone in records:
|
||||
ht_insert(buckets, name, phone)
|
||||
end = time.perf_counter()
|
||||
else:
|
||||
root = None
|
||||
start = time.perf_counter()
|
||||
for name, phone in records:
|
||||
root = bst_insert(root, name, phone)
|
||||
end = time.perf_counter()
|
||||
times.append(end - start)
|
||||
return times
|
||||
|
||||
def build_structure(struct_type, records):
|
||||
if struct_type == 'll':
|
||||
head = None
|
||||
for name, phone in records:
|
||||
head = ll_insert(head, name, phone)
|
||||
return head
|
||||
elif struct_type == 'ht':
|
||||
buckets = ht_create(2000)
|
||||
for name, phone in records:
|
||||
ht_insert(buckets, name, phone)
|
||||
return buckets
|
||||
else:
|
||||
root = None
|
||||
for name, phone in records:
|
||||
root = bst_insert(root, name, phone)
|
||||
return root
|
||||
|
||||
def measure_search(struct_type, structure, records):
|
||||
times = []
|
||||
N_records = len(records)
|
||||
for _ in range(REPEATS):
|
||||
indices = random.sample(range(N_records), 100)
|
||||
existing_names = [records[i][0] for i in indices]
|
||||
missing_names = [f"None_{i}" for i in range(10)]
|
||||
search_names = existing_names + missing_names
|
||||
random.shuffle(search_names)
|
||||
start = time.perf_counter()
|
||||
if struct_type == 'll':
|
||||
for name in search_names:
|
||||
ll_find(structure, name)
|
||||
elif struct_type == 'ht':
|
||||
for name in search_names:
|
||||
ht_find(structure, name)
|
||||
else:
|
||||
for name in search_names:
|
||||
bst_find(structure, name)
|
||||
times.append(time.perf_counter() - start)
|
||||
return times
|
||||
|
||||
def measure_deletion(struct_type, records):
|
||||
times = []
|
||||
N_records = len(records)
|
||||
for _ in range(REPEATS):
|
||||
indices = random.sample(range(N_records), 50)
|
||||
delete_names = [records[i][0] for i in indices]
|
||||
if struct_type == 'll':
|
||||
head = None
|
||||
for name, phone in records:
|
||||
head = ll_insert(head, name, phone)
|
||||
start = time.perf_counter()
|
||||
for name in delete_names:
|
||||
head = ll_delete(head, name)
|
||||
end = time.perf_counter()
|
||||
elif struct_type == 'ht':
|
||||
buckets = ht_create(2000)
|
||||
for name, phone in records:
|
||||
ht_insert(buckets, name, phone)
|
||||
start = time.perf_counter()
|
||||
for name in delete_names:
|
||||
ht_delete(buckets, name)
|
||||
end = time.perf_counter()
|
||||
else:
|
||||
root = None
|
||||
for name, phone in records:
|
||||
root = bst_insert(root, name, phone)
|
||||
start = time.perf_counter()
|
||||
for name in delete_names:
|
||||
root = bst_delete(root, name)
|
||||
end = time.perf_counter()
|
||||
times.append(end - start)
|
||||
return times
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user