-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspeed_test.py
More file actions
75 lines (56 loc) · 2.1 KB
/
speed_test.py
File metadata and controls
75 lines (56 loc) · 2.1 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
#!/usr/bin/env python3
"""
Speed comparison test for different demo modes
"""
import time
import sys
import os
import pygame
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from main import (bubble_sort, insertion_sort, quick_sort, generate_list,
DrawInformation)
def time_sorting_algorithm(algorithm, draw_info, name):
"""Time how long it takes to complete a sorting algorithm"""
start_time = time.time()
# Run the algorithm without visualization
sorting_generator = algorithm(draw_info, True)
try:
while True:
next(sorting_generator)
except StopIteration:
pass
end_time = time.time()
return end_time - start_time
def speed_test():
"""Test the speed of different array sizes"""
pygame.init()
print("🚀 Speed Test - Sorting Algorithm Performance")
print("=" * 50)
# Test different array sizes
test_sizes = [15, 25, 40, 60]
algorithms = [
(bubble_sort, "Bubble Sort"),
(insertion_sort, "Insertion Sort"),
(quick_sort, "Quick Sort")
]
for size in test_sizes:
print(f"\n📊 Array Size: {size}")
print("-" * 30)
# Generate test array
lst = generate_list(size, 1, 100)
draw_info = DrawInformation(800, 600, lst)
for algorithm, name in algorithms:
# Reset the array for each algorithm
draw_info.set_list(lst[:]) # Copy the list
# Time the algorithm
duration = time_sorting_algorithm(algorithm, draw_info, name)
# Calculate operations
comparisons = draw_info.stats.comparisons
swaps = draw_info.stats.swaps
print(f"{name:15} | {duration:6.3f}s | {comparisons:4d} comp | {swaps:4d} swaps")
print(f"\n💡 For fastest demo, use: python demo.py --express")
print(f"💡 For normal fast demo, use: python demo.py")
print(f"💡 For detailed demo, increase array size in demo.py")
pygame.quit()
if __name__ == "__main__":
speed_test()