-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjudy_bench.jl
179 lines (154 loc) · 4.35 KB
/
judy_bench.jl
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
using DataStructures
include("./judydict.jl")
Nm=1000*1000
test_keys = Vector{Int64}(Nm)
test_vals = Vector{Int64}(Nm)
test_keys2 = Vector{Int64}(Nm)
for i in 1:Nm
test_keys[i] = rand(1:2^60)
test_keys2[i] = rand(1:2^60)
test_vals[i] = rand(1:2^60)
end
function store_test(sd, keys_,vals_)
for i in 1:length(keys_)
sd[keys_[i]]=vals_[i]
end
return nothing
end
function read_test(sd,keys_,vals_)
sum::Int64 = 0
for i in 1:length(keys_)
sdv = sd[keys_[i]]
assert(sdv == vals_[i])
end
return sum
end
function del_test(sd,keys_)
for i in 1:length(keys_)
delete!(sd, keys_[i])
end
return nothing
end
function findnext_test(sd::SortedDict,keys_)
for i in 1:length(keys_)
searchsortedfirst(sd,keys_[i])
end
return nothing
end
function findnext_test(sd::judydict.JudyDict,keys_)
for i in 1:length(keys_)
judydict.ju_next(sd,keys_[i])
end
return nothing
end
function iter_test(sd::SortedDict, keys_)
idx::Int64 = 0
num_iter = 0
for (key_, value_) in sd
num_iter += 1
end
assert(num_iter == length(keys_))
return num_iter
end
function iter_test(jd::judydict.JudyDict,keys_)
idx::Int64 = 0
num_iter = 0
vptr:: Ptr{Int}=C_NULL
(idx, vptr) = judydict.ju_next_inclusive(jd, idx)
while vptr != C_NULL
(idx, vptr) = judydict.ju_next(jd, idx)
num_iter +=1
end
assert(num_iter == length(keys_))
return num_iter
end
function iter_test(usd::Dict,keys_)
idx::Int64 = 0
num_iter = 0
for (key_, value_) in usd
num_iter += 1
end
assert(num_iter == length(keys_))
return num_iter
end
#######################################
println("Trivial benchmark with ", Nm, " elements")
sd = SortedDict{Int64, Int64}()
store_test(sd, test_keys, test_vals)
read_test(sd, test_keys, test_vals)
findnext_test(sd, test_keys2)
iter_test(sd, test_keys)
del_test(sd, test_keys)
sd = 0
gc()
println("Timings for SortedDict{Int64,Int64}, setindex!, getindex, findnext, iterate, delete!")
sd = SortedDict{Int64, Int64}()
@time store_test(sd, test_keys, test_vals)
@time read_test(sd, test_keys, test_vals)
@time findnext_test(sd, test_keys2)
@time iter_test(sd, test_keys)
@time del_test(sd, test_keys)
sd = 0
gc()
println("Timings for SortedDict{Int64,Int64}, setindex!, getindex, findnext, iterate, delete!")
sd = SortedDict{Int64, Int64}()
@time store_test(sd, test_keys, test_vals)
@time read_test(sd, test_keys, test_vals)
@time findnext_test(sd, test_keys2)
@time iter_test(sd, test_keys)
@time del_test(sd, test_keys)
sd = 0
#######################################
jd = judydict.JudyDict()
store_test(jd, test_keys, test_vals)
read_test(jd, test_keys, test_vals)
mem_used = judydict.ju_mem_used(jd)
findnext_test(jd, test_keys2)
iter_test(jd, test_keys)
del_test(jd, test_keys)
jd = 0
gc()
println("Timings for judydict, setindex!, getindex, findnext, iterate, delete!")
jd = judydict.JudyDict()
@time store_test(jd, test_keys, test_vals)
@time read_test(jd, test_keys, test_vals)
mem_used = judydict.ju_mem_used(jd)
@time findnext_test(jd, test_keys2)
@time iter_test(jd, test_keys)
@time del_test(jd, test_keys)
println("reported memory usage: ", mem_used)
jd = 0
gc()
println("Timings for judydict, setindex!, getindex, findnext, iterate, delete!")
jd = judydict.JudyDict()
@time store_test(jd, test_keys, test_vals)
@time read_test(jd, test_keys, test_vals)
mem_used = judydict.ju_mem_used(jd)
@time findnext_test(jd, test_keys2)
@time iter_test(jd, test_keys)
@time del_test(jd, test_keys)
println("reported memory usage: ", mem_used)
jd = 0
#######################################
usd = Dict{Int64, Int64}()
store_test(usd, test_keys, test_vals)
read_test(usd, test_keys, test_vals)
iter_test(usd, test_keys)
del_test(usd, test_keys)
usd = 0
gc()
println("Timings for Dict{Int64,Int64}, setindex!, getindex, iterate, delete!")
usd = Dict{Int64, Int64}()
@time store_test(usd, test_keys, test_vals)
@time read_test(usd, test_keys, test_vals)
@time iter_test(usd, test_keys)
@time del_test(usd, test_keys)
gc()
gc()
println("Timings for Dict{Int64,Int64}, setindex!, getindex, iterate, delete!")
usd = Dict{Int64, Int64}()
@time store_test(usd, test_keys, test_vals)
@time read_test(usd, test_keys, test_vals)
@time iter_test(usd, test_keys)
@time del_test(usd, test_keys)
gc()