-
Notifications
You must be signed in to change notification settings - Fork 34
/
heap_speed_test.py
45 lines (37 loc) · 1.01 KB
/
heap_speed_test.py
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
import sys
import random
import heap
def test_with_heap(size):
priority_queue = heap.Heap()
for i in range(size):
rand = random.randint(1, size)
priority_queue.push(rand)
top10 = [priority_queue.pop() for _ in range(10)]
print("Top 10 (with heap):", top10)
def test_with_sort(size):
data = []
for i in range(size):
rand = random.randint(1, size)
data.append(i)
sorted_data = sorted(data, reverse=True)
top10 = sorted_data[0:10]
print("Top 10 (with sort):", top10)
def test_with_max(size):
data = []
for i in range(size):
rand = random.randint(1, size)
data.append(i)
top10 = []
for i in range(10):
m = max(data)
data.remove(m)
top10.append(m)
print("Top 10 (with sort):", top10)
if __name__ == "__main__":
size = 1000000
if sys.argv[1] == 'heap':
test_with_heap(size)
if sys.argv[1] == 'sort':
test_with_heap(size)
if sys.argv[1] == 'max':
test_with_heap(size)