[1] add correctly results
This commit is contained in:
parent
f25d39d59e
commit
6adf9d0516
|
|
@ -1,3 +1,9 @@
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
import csv
|
||||||
|
import sys
|
||||||
|
sys.setrecursionlimit(20000)
|
||||||
|
|
||||||
def ll_insert(head, name, phone):
|
def ll_insert(head, name, phone):
|
||||||
cur = head
|
cur = head
|
||||||
while cur:
|
while cur:
|
||||||
|
|
@ -46,7 +52,7 @@ def ll_list_all(head):
|
||||||
res.sort(key=lambda x: x[0])
|
res.sort(key=lambda x: x[0])
|
||||||
return res
|
return res
|
||||||
|
|
||||||
SIZE = 5
|
SIZE = 10
|
||||||
|
|
||||||
def hash_func(name):
|
def hash_func(name):
|
||||||
s = 0
|
s = 0
|
||||||
|
|
@ -144,9 +150,6 @@ def bst_list_all(root):
|
||||||
inorder(root)
|
inorder(root)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
import random, time, csv, sys
|
|
||||||
sys.setrecursionlimit(20000)
|
|
||||||
|
|
||||||
def generate_records(n, seed=42):
|
def generate_records(n, seed=42):
|
||||||
random.seed(seed)
|
random.seed(seed)
|
||||||
rec = []
|
rec = []
|
||||||
|
|
@ -162,7 +165,7 @@ def prepare_datasets(base):
|
||||||
sorted_rec = sorted(base, key=lambda x: x[0])
|
sorted_rec = sorted(base, key=lambda x: x[0])
|
||||||
return shuffled, sorted_rec
|
return shuffled, sorted_rec
|
||||||
|
|
||||||
def run_experiment(funcs, records, mode, repeats=3):
|
def run_experiment(funcs, records, mode, repeats=5):
|
||||||
results = []
|
results = []
|
||||||
for rep in range(repeats):
|
for rep in range(repeats):
|
||||||
struct = funcs['create']()
|
struct = funcs['create']()
|
||||||
|
|
@ -170,39 +173,42 @@ def run_experiment(funcs, records, mode, repeats=3):
|
||||||
for name, phone in records:
|
for name, phone in records:
|
||||||
struct = funcs['insert'](struct, name, phone)
|
struct = funcs['insert'](struct, name, phone)
|
||||||
t1 = time.perf_counter()
|
t1 = time.perf_counter()
|
||||||
ins = t1 - t0
|
insert_time = t1 - t0
|
||||||
|
|
||||||
existing = [n for n, _ in records]
|
existing = [n for n, _ in records]
|
||||||
sample = random.sample(existing, 100)
|
sample_existing = random.sample(existing, 100)
|
||||||
non_ex = [f"None_{i}" for i in range(10)]
|
non_existing = [f"None_{i}" for i in range(10)]
|
||||||
to_search = sample + non_ex
|
search_names = sample_existing + non_existing
|
||||||
random.shuffle(to_search)
|
random.shuffle(search_names)
|
||||||
t0 = time.perf_counter()
|
t0 = time.perf_counter()
|
||||||
for name in to_search:
|
for name in search_names:
|
||||||
_ = funcs['find'](struct, name)
|
_ = funcs['find'](struct, name)
|
||||||
t1 = time.perf_counter()
|
t1 = time.perf_counter()
|
||||||
find_t = t1 - t0
|
search_time = t1 - t0
|
||||||
to_del = random.sample(existing, 10)
|
|
||||||
|
to_delete = random.sample(existing, 50)
|
||||||
t0 = time.perf_counter()
|
t0 = time.perf_counter()
|
||||||
for name in to_del:
|
for name in to_delete:
|
||||||
struct = funcs['delete'](struct, name)
|
struct = funcs['delete'](struct, name)
|
||||||
t1 = time.perf_counter()
|
t1 = time.perf_counter()
|
||||||
del_t = t1 - t0
|
delete_time = t1 - t0
|
||||||
|
|
||||||
results.append({
|
results.append({
|
||||||
'structure': funcs['name'],
|
'structure': funcs['name'],
|
||||||
'mode': mode,
|
'mode': mode,
|
||||||
'rep': rep+1,
|
'repetition': rep+1,
|
||||||
'insert': ins,
|
'insert': insert_time,
|
||||||
'search': find_t,
|
'search': search_time,
|
||||||
'delete': del_t
|
'delete': delete_time
|
||||||
})
|
})
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
N = 1000
|
N = 1000
|
||||||
base = generate_records(N)
|
base_records = generate_records(N)
|
||||||
shuffled, sorted_rec = prepare_datasets(base)
|
shuffled, sorted_records = prepare_datasets(base_records)
|
||||||
|
|
||||||
structs = {
|
structures = {
|
||||||
'LinkedList': {
|
'LinkedList': {
|
||||||
'name': 'LinkedList',
|
'name': 'LinkedList',
|
||||||
'create': lambda: None,
|
'create': lambda: None,
|
||||||
|
|
@ -229,20 +235,27 @@ def main():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
all_res = []
|
all_results = []
|
||||||
for name, funcs in structs.items():
|
for name, funcs in structures.items():
|
||||||
print(f"Testing {name} random...")
|
print(f"Testing {name} on random order...")
|
||||||
all_res += run_experiment(funcs, shuffled, 'random')
|
all_results.extend(run_experiment(funcs, shuffled, 'random'))
|
||||||
print(f"Testing {name} sorted...")
|
print(f"Testing {name} on sorted order...")
|
||||||
all_res += run_experiment(funcs, sorted_rec, 'sorted')
|
all_results.extend(run_experiment(funcs, sorted_records, 'sorted'))
|
||||||
|
|
||||||
with open('results.csv', 'w', newline='', encoding='utf-8') as f:
|
with open('results.csv', 'w', newline='', encoding='utf-8') as f:
|
||||||
writer = csv.writer(f)
|
writer = csv.writer(f)
|
||||||
writer.writerow(['Structure','Mode','Repeat','Insert','Search','Delete'])
|
writer.writerow(['Structure', 'Mode', 'Repetition', 'Insert (sec)', 'Search (sec)', 'Delete (sec)'])
|
||||||
for r in all_res:
|
for r in all_results:
|
||||||
writer.writerow([r['structure'], r['mode'], r['rep'],
|
writer.writerow([
|
||||||
f"{r['insert']:.6f}", f"{r['search']:.6f}", f"{r['delete']:.6f}"])
|
r['structure'],
|
||||||
print("Done. Results in results.csv")
|
r['mode'],
|
||||||
|
r['repetition'],
|
||||||
|
f"{r['insert']:.6f}",
|
||||||
|
f"{r['search']:.6f}",
|
||||||
|
f"{r['delete']:.6f}"
|
||||||
|
])
|
||||||
|
|
||||||
|
print("Experiment finished. Results saved to esults.csv")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
Loading…
Reference in New Issue
Block a user