-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
154 lines (136 loc) · 3.97 KB
/
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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
Tests for SkipList and RedBlackTree. It tests with different type of data (ordered, reversed,
and randomized) with different amount of data. Of course you should remember, that even if general,
last results are usually the same (x is better than y), percentage resulst can vary very much
(it depends on os and python itself I guess) even on the same machine.
"""
import time, random
import numpy.random as R
from tree import RedBlackTree
from skiplist import SkipList
SEARCH_AMOUNT = 10
RANDOM_DATA_LOOPS = 3
def test():
"""
General test form SkipList and RedBlackTree.
"""
general = {"list_insert": 0, "list_search": 0, "tree_insert": 0, "tree_search": 0}
# Amount is given so SkipList should handle it without problems with its MAX_LEVEL.
for amount in [1000, 10000, 100000]:
print "Test for amount of data: " + str(amount)
print
skiplist = SkipList()
tree = RedBlackTree()
data = range(0, amount)
reverseddata = range(amount, 0, -1)
randoms = []
amount2 = amount * 2
for i in range(0, SEARCH_AMOUNT):
"Each time we test value that we now is in structure and one that isn't."
randoms.append(R.randint(0, amount))
randoms.append(R.randint(amount, amount2))
print "Test case for ordered data."
print
testInserting(skiplist, tree, data, general)
print
testSearching(skiplist, tree, data, general)
print
print "Test case for reversed data."
print
testInserting(skiplist, tree, reverseddata, general)
print
testSearching(skiplist, tree, reverseddata, general)
print
for loop in range(0, RANDOM_DATA_LOOPS):
random.shuffle(data)
print "Test case for random data (attempt " + str(loop + 1) + "/" + str(RANDOM_DATA_LOOPS) + ")"
print
testInserting(skiplist, tree, reverseddata, general)
print
testSearching(skiplist, tree, reverseddata, general)
print
print "---------------------"
print
listtime = general['list_insert']
treetime = general['tree_insert']
print "Inserting in general:"
print "SkipList: " + str(listtime)
print "RedBlackTree: " + str(treetime)
if listtime < treetime:
msg = "SkipList"
result = treetime / listtime * 100
else:
msg = "RedBlackTree"
result = listtime / treetime * 100
result -= 100
msg += " is better for about " + str(int(result)) + "%"
print msg
print
listtime = general['list_search']
treetime = general['tree_search']
print "Searching in general:"
print "SkipList: " + str(listtime)
print "RedBlackTree: " + str(treetime)
if listtime < treetime:
msg = "SkipList"
result = treetime / listtime * 100
else:
msg = "RedBlackTree"
result = listtime / treetime * 100
result -= 100
msg += " is better for about " + str(int(result)) + "%"
print msg
def testInserting(skiplist, tree, data, general):
"""
Case for inserting data.
"""
start = time.time()
for i in data:
skiplist.insert(i)
listtime = time.time() - start
general['list_insert'] += listtime
start = time.time()
for i in data:
tree.insert(i)
treetime = time.time() - start
general['tree_insert'] += treetime
print "Inserting:"
print "SkipList: " + str(listtime)
print "RedBlackTree: " + str(treetime)
if listtime < treetime:
msg = "SkipList"
result = treetime / listtime * 100
else:
msg = "RedBlackTree"
result = listtime / treetime * 100
result -= 100
msg += " is better for about " + str(int(result)) + "%"
print msg
def testSearching(skiplist, tree, data, general):
"""
Case for searching data.
"""
start = time.time()
for i in data:
skiplist.search(i)
listtime = time.time() - start
general['list_search'] += listtime
start = time.time()
for i in data:
tree.search(i)
treetime = time.time() - start
general['tree_search'] += treetime
print "Searching:"
print "SkipList: " + str(listtime)
print "RedBlackTree: " + str(treetime)
if listtime < treetime:
msg = "SkipList"
result = treetime / listtime * 100
else:
msg = "RedBlackTree"
result = listtime / treetime * 100
result -= 100
msg += " is better for about " + str(int(result)) + "%"
print msg
if __name__ == "__main__":
test()