Repository navigation
Expand file tree
/
Copy pathbenchmark.py
More file actions
56 lines (42 loc) · 1.52 KB
/
Copy pathbenchmark.py
File metadata and controls
56 lines (42 loc) · 1.52 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
import time, testcase
import numpy as np
import sort_algorithm.heapsort as heapsort
import sort_algorithm.quicksort as quicksort
import sort_algorithm.mergesort as mergesort
# Tập dữ liệu test
dataset = testcase.test_case
# Danh sách các thuật toán sắp xếp cần benchmark
dict = [
("Heap Sort", heapsort),
("Quick Sort", quicksort),
("Merge Sort", mergesort),
("Numpy Sort", np)
]
speed_time = [] # Lưu thời gian chạy của từng thuật toán
# Lặp qua từng thuật toán
for sort_type in range(4):
object = dict[sort_type]
print(f"Testing {object[0]} ....")
counter = 0
speed_ds = [] # Lưu thời gian của từng dataset
# Chạy thuật toán trên từng bộ dữ liệu
for current_dataset in dataset:
counter += 1
# IMPORTANT: copy dataset because numpy arrays are mutable
data_copy = current_dataset.copy()
# Bắt đầu đo thời gian
current_tick = time.time()
if object[0] == "Numpy Sort":
object[1].sort(data_copy)
else:
object[1].run(data_copy)
# Tính thời gian chạy (ms)
runtime = int((time.time() - current_tick) * 1000)
print(f"Dataset #{counter} took {runtime} ms!")
speed_ds.append(runtime)
# Lưu kết quả của thuật toán hiện tại
speed_time.append(speed_ds)
# In thời gian chạy trung bình
print(f"Average Timelimit: {sum(speed_ds) / 10}\n")
# In toàn bộ kết quả benchmark
print(speed_time)