-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab1_plot_sort_time.py
More file actions
101 lines (83 loc) · 3.91 KB
/
Lab1_plot_sort_time.py
File metadata and controls
101 lines (83 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import os
import csv
import matplotlib.pyplot as plt
input_dir = "children-sorted/times"
output_dir = "plots1"
os.makedirs(output_dir, exist_ok=True)
# Создаем словарь, в котором будем хранить данные(название сортировки -> [(размер1, время1), ...])
sort_results = {}
# Парсим название файла, извлекаем размер
def extract_size(filename):
return int(filename.split('_')[-1].replace('.csv', ''))
for filename in os.listdir(input_dir):
# Извлекаем размер из имени файла: "times-children_XXX.csv"
size_str = extract_size(filename)
size = int(size_str)
filepath = os.path.join(input_dir, filename)
with open(filepath, newline='', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
sort_name = row[0]
time_ms = float(row[1])
if sort_name in sort_results:
sort_results[sort_name].append((size, time_ms))
else:
sort_results[sort_name]=[(size, time_ms), ]
# Сортируем точки графика по размеру для каждой из сортировок
for sort_name in sort_results:
sort_results[sort_name].sort()
# Рисуем все сортировки на 1 графике (обычном)
plt.figure(figsize=(10, 6))
for sort_name, data in sort_results.items():
sizes = [x[0] for x in data]
times = [x[1] for x in data]
plt.plot(sizes, times, label=sort_name, marker='o')
plt.title("Время сортировки vs Размер массива (общий график)")
plt.xlabel("Размер массива")
plt.ylabel("Время (мс)")
plt.grid(True)
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(output_dir, "plot-all.png"))
# Рисуем все сортировки на 1 графике (логарифмическом)
plt.figure(figsize=(10, 6))
for sort_name, data in sort_results.items():
sizes = [x[0] for x in data]
times = [x[1] for x in data]
plt.plot(sizes, times, label=sort_name, marker='o')
plt.title("Время сортировки vs Размер массива (логарифмическая шкала)")
plt.xlabel("Размер массива")
plt.ylabel("Время (мс, лог)")
plt.yscale("log") # Логарифмическая шкала по y
plt.grid(True, which="both", linestyle='--', linewidth=0.5)
plt.legend()
plt.tight_layout()
plt.savefig(os.path.join(output_dir, "plot-all-log.png"))
# Рисуем каждую сортировку на отдельном графике (обычном)
for sort_name, data in sort_results.items():
sizes = [x[0] for x in data]
times = [x[1] for x in data]
plt.figure(figsize=(8, 5))
plt.plot(sizes, times, marker='o', color='teal')
plt.title(f"{sort_name}: время сортировки vs размер")
plt.xlabel("Размер массива")
plt.ylabel("Время (мс)")
plt.grid(True)
plt.tight_layout()
safe_name = sort_name.lower().replace(" ", "_")
plt.savefig(os.path.join(output_dir, f"plot-{safe_name}.png"))
# Рисуем каждую сортировку на отдельном графике (логарифмическом)
for sort_name, data in sort_results.items():
sizes = [x[0] for x in data]
times = [x[1] for x in data]
plt.figure(figsize=(8, 5))
plt.plot(sizes, times, marker='o', color='teal')
plt.title(f"{sort_name}: время сортировки vs размер (логарифм)")
plt.xlabel("Размер массива")
plt.ylabel("Время (мс, лог)")
plt.yscale("log") # Логарифмическая шкала по y
plt.grid(True, which="both", linestyle='--', linewidth=0.5)
plt.tight_layout()
safe_name = sort_name.lower().replace(" ", "_")
plt.savefig(os.path.join(output_dir, f"plot-{safe_name}-log.png"))
plt.close()