2026-04-19 17:56:50 +00:00
|
|
|
import csv
|
|
|
|
|
import random
|
|
|
|
|
import sys
|
2026-04-20 23:51:51 +00:00
|
|
|
from pathlib import Path
|
2026-04-19 17:56:50 +00:00
|
|
|
|
2026-04-20 23:51:51 +00:00
|
|
|
from .generator import generate_records, shuffle_records, sort_records, sample_existing, sample_nonexistent
|
|
|
|
|
from .bench import run_once
|
2026-04-19 17:56:50 +00:00
|
|
|
|
|
|
|
|
N = 10000
|
|
|
|
|
RUNS = 5
|
|
|
|
|
SEARCH_K = 100
|
|
|
|
|
SEARCH_MISSING_K = 10
|
|
|
|
|
DELETE_K = 50
|
|
|
|
|
sys.setrecursionlimit(15000)
|
2026-04-20 23:51:51 +00:00
|
|
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
|
|
|
|
RESULT_PATH = BASE_DIR / "results.csv"
|
2026-04-19 17:56:50 +00:00
|
|
|
|
|
|
|
|
def run_experiment(records, mode):
|
|
|
|
|
search_names = sample_existing(records, SEARCH_K) + sample_nonexistent(SEARCH_MISSING_K)
|
|
|
|
|
delete_names = sample_existing(records, DELETE_K)
|
|
|
|
|
|
|
|
|
|
all_rows = []
|
|
|
|
|
|
|
|
|
|
for run_i in range(1, RUNS + 1):
|
|
|
|
|
print(f" [{mode}] run {run_i}/{RUNS} ...")
|
|
|
|
|
run_results = run_once(records, search_names, delete_names)
|
|
|
|
|
for label, t_insert, t_find, t_delete in run_results:
|
|
|
|
|
all_rows.append([label, mode, 'insert', run_i, round(t_insert, 6)])
|
|
|
|
|
all_rows.append([label, mode, 'find', run_i, round(t_find, 6)])
|
|
|
|
|
all_rows.append([label, mode, 'delete', run_i, round(t_delete, 6)])
|
|
|
|
|
|
|
|
|
|
return all_rows
|
|
|
|
|
|
|
|
|
|
|
2026-04-20 23:51:51 +00:00
|
|
|
def main_experiment():
|
2026-04-19 17:56:50 +00:00
|
|
|
random.seed(52)
|
|
|
|
|
|
|
|
|
|
records_base = generate_records(N)
|
|
|
|
|
records_shuffled = shuffle_records(records_base)
|
|
|
|
|
records_sorted = sort_records(records_base)
|
|
|
|
|
|
|
|
|
|
rows = []
|
|
|
|
|
rows += run_experiment(records_shuffled, 'shuffled')
|
|
|
|
|
rows += run_experiment(records_sorted, 'sorted')
|
|
|
|
|
|
|
|
|
|
header = ['structure', 'mode', 'operation', 'run', 'time_sec']
|
2026-04-20 23:51:51 +00:00
|
|
|
output_path = RESULT_PATH
|
2026-04-19 17:56:50 +00:00
|
|
|
|
|
|
|
|
with open(output_path, 'w', newline='') as f:
|
|
|
|
|
writer = csv.writer(f)
|
|
|
|
|
writer.writerow(header)
|
|
|
|
|
writer.writerows(rows)
|
|
|
|
|
|
2026-04-20 23:51:51 +00:00
|
|
|
print(f"Done. Results saved to {output_path.name}")
|
2026-04-19 17:56:50 +00:00
|
|
|
print(f"Total rows: {len(rows)}")
|
2026-04-20 23:51:51 +00:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
main_experiment()
|