-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
99 lines (72 loc) · 2.44 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
from flexible_skiplist import skiplist
from random import randint
# NB! Can flake as neither sort order is stable. Keep maxval high so values are unlikely to repeat
MAXVAL = 100000
N, M = 100, 100
def test_sorted_skiplist():
# Create an indexed list of random integers
lst = [ (i,randint(0,MAXVAL)) for i in range(N)]
# Sort them by the random value
sl = skiplist([], key=lambda x: x[1], cumulate_field_fns=[lambda x: x[1]])
# insert list into skiplist
for v in reversed(lst):
sl.insert(v)
# check length
assert len(lst) == len(sl)
# check if it is sorted properly
slst = sorted(lst,key=lambda x:x[1])
for i, v in enumerate(slst):
assert v == sl[i]
# Initialize with full list
sl = skiplist(lst, key=lambda x: x[1], cumulate_field_fns=[lambda x: x[1]])
# Again, check if it is sorted properly
for i, v in enumerate(slst):
assert v[1] == sl[i][1]
# Perform random insertions
for i in range(N,N+M):
val = (i,randint(0,MAXVAL))
sl.insert(val)
lst.append(val)
# Check if still sorted properly
slst = sorted(lst,key=lambda x:x[1])
for i, v in enumerate(slst):
assert v[1] == sl[i][1]
# Perform random deletions
for i in range(M):
ind = randint(0,len(lst)-1)
val = lst[ind]
sl.remove(val)
del lst[ind]
# Check if still sorted properly
slst = sorted(lst,key=lambda x:x[1])
for i, v in enumerate(slst):
assert v[1] == sl[i][1]
# Create an indexed list of constant 0 values
lst = [ (i,0) for i in range(N)]
sl = skiplist(lst,key=lambda x: x[1])
# Perform random deletions
for i in range(N-1):
ind = randint(0,len(lst)-1)
val = lst[ind]
sl.remove(val)
del lst[ind]
# Check that the right element remains
assert lst[0] == sl[0]
test_sorted_skiplist()
from time import perf_counter
for N in [4,16,64,256,1024,4096]:
lst = [ (i,randint(0,MAXVAL)) for i in range(N)]
beg_time = perf_counter()
for _ in range(M):
sl = skiplist(lst, cumulate_field_fns=[lambda x: x[1]])
time = (perf_counter()-beg_time)
print("%5i: Time: %.6f (%.7f)" % (N,time/M,time/(M*N)))
beg_time = perf_counter()
for _ in range(M):
sl = skiplist([], cumulate_field_fns=[lambda x: x[1]])
# Doing it in reverse is faster because links are simpler to handle
li = len(lst)-1
for i,val in enumerate(reversed(lst)):
sl._insert_after_path(val,sl._gen_level(li-i))
time = (perf_counter()-beg_time)
print("%5i: Time: %.6f (%.7f)" % (N,time/M,time/(M*N)))