-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_test.py
More file actions
38 lines (30 loc) · 1.12 KB
/
performance_test.py
File metadata and controls
38 lines (30 loc) · 1.12 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
import time
import random
from quicksort import quicksort
def test_performance(size=1000, runs=5):
print(f"Testing performance with arrays of size {size}")
total_custom = 0
total_builtin = 0
for i in range(runs):
# Generate random array
test_array = [random.randint(0, 10000) for _ in range(size)]
test_array_copy = test_array.copy()
# Test custom quicksort
start = time.time()
quicksort(test_array)
end = time.time()
custom_time = end - start
total_custom += custom_time
# Test Python's built-in sort
start = time.time()
sorted(test_array_copy)
end = time.time()
builtin_time = end - start
total_builtin += builtin_time
print(f"Run {i+1}: Custom quicksort: {custom_time:.6f}s, Built-in sort: {builtin_time:.6f}s")
print(f"\nAverage over {runs} runs:")
print(f"Custom quicksort: {total_custom/runs:.6f}s")
print(f"Built-in sort: {total_builtin/runs:.6f}s")
if __name__ == "__main__":
test_performance()
test_performance(size=10000, runs=3)