-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
222 lines (167 loc) · 4.98 KB
/
tests.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from skip_list import SkipList
from bst import BST
import time
import json
import logging
from random import randrange
logging.basicConfig(
format='[%(levelname)s][%(asctime)s]: %(message)s', level=logging.INFO)
# Define a runner for the Skip List
def insertion_run(size):
# Create an Skip List
sl = SkipList()
# Now, get time
start = time.clock()
# Insert elements
for i in xrange(size):
sl.insert(i)
# Get final time, and store
end = time.clock()
return {
'structure': 'skip_list',
'size': size,
'time': end - start,
'comparisons': sl.comparisons,
'height': sl.maxHeight
}
def search_run(size):
# Create an Skip List
sl = SkipList()
# Insert elements
for i in xrange(size):
sl.insert(i)
# Now, get time
start = time.clock()
# Find elements
for i in xrange(size):
sl.find(size)
# Get final time, and store
end = time.clock()
return {
'structure': 'skip_list',
'size': size,
'time': end - start,
'comparisons': sl.comparisons,
'height': sl.maxHeight
}
# Define runs fot BST
def bst_insert_run(data):
pass
def bst_search_run(data):
# Create a BST
bst = BST()
# Insert elements
for i in xrange(size):
bst.insert(i)
# Now, get time
start = time.clock()
# Find elements
for i in data:
bst.find(i)
# Get final time, and store
end = time.clock()
return {
'structure': 'bst',
'size': size,
'time': end - start,
'comparisons': bst.comparisons
}
def swap(data):
assert len(data) > 2
# Choose two random indexes
ii = randrange(0, len(data))
jj = randrange(0, len(data))
# Swap
data[ii], data[jj] = data[jj], data[ii]
return data
# Run many times
n = 100
sizes = [10**4, 2 * 10 ** 4, 5 * 10 ** 4]
constructors = {
'bst': BST,
'skip_list': SkipList
}
for structure in ['bst', 'skip_list']:
logging.info("Starting %s structure tests" % structure)
constructor = constructors[structure]
# Now, for each size...
for size in sizes:
# CREATE INPUT
data = range(size)
fail_data = range(size, size * 2)
insert_results = []
search_results = []
# Now, for each n that we will run...
for ni in xrange(n):
# SWAPS
if structure == 'bst':
k = int(round(0.005 * float(size)))
# Perform swaps
for i in range(k):
swap(data)
# And insert data
# Create a BST
bst = constructor()
# Now, get time
start = time.clock()
# Insert elements
for i in data:
bst.insert(i)
# Get final time, and store
end = time.clock()
insert_result = {
'structure': structure,
'size': len(data),
'n': ni,
'time': end - start,
'comparisons': bst.comparisons,
}
if structure == 'bst':
insert_result['swaps'] = k
print(insert_result)
# Reset comparisons counter
bst.comparisons = 0
# Now, perform queries
# Size of que search
search_size = int(round(0.5 * float(size)))
fail_size = int(round(0.25 * float(search_size)))
useful_size = search_size - fail_size
# Form data to search
search_data = []
# And populate
for i in xrange(useful_size):
# Pick random from data
ir = randrange(0, size)
search_data.append(data[ir])
for i in xrange(fail_size):
ir = randrange(0, size)
search_data.append(fail_data[ir])
# Perform searchs
# Now, get time
start = time.clock()
# Find elements
for i in data:
bst.find(i)
# Get final time, and store
end = time.clock()
search_result = {
'structure': structure,
'size': size,
'n': ni,
'time': end - start,
'comparisons': bst.comparisons
}
print(search_result)
# Store results
insert_results.append(insert_result)
search_results.append(search_result)
# Dump things
file_name = 'dumps/dump-%s-%d-%s.json' % (structure, size, 'insert')
with open(file_name, 'w') as f:
json.dump(insert_results, f, indent=2)
# Dump results
file_name = 'dumps/dump-%s-%d-%s.json' % (structure, size, 'search')
with open(file_name, 'w') as f:
json.dump(search_results, f, indent=2)
logging.info("Size: %sd done :D!" % size)
logging.info("Structure: %s done :D!" % structure)