-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_generic_dict.mojo
167 lines (134 loc) · 4.67 KB
/
benchmark_generic_dict.mojo
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
import benchmark
from generic_dict import Dict, Keyable, KeysBuilder
from collections.dict import KeyElement, Dict as StdDict
from pathlib import cwd
from testing import assert_equal
from corpora import *
@value
struct StringKey(KeyElement, Keyable):
var s: String
fn __init__(inout self, owned s: String):
self.s = s^
fn __init__(inout self, s: StringLiteral):
self.s = String(s)
fn __hash__(self) -> Int:
var ptr = self.s.unsafe_ptr()
return hash(ptr, len(self.s))
fn __eq__(self, other: Self) -> Bool:
return self.s == other.s
fn __ne__(self, other: Self) -> Bool:
return self.s != other.s
fn accept[T: KeysBuilder](self, inout keys_builder: T):
keys_builder.add_buffer(self.s.unsafe_ptr(), len(self.s))
fn corpus_stats(corpus: List[String]):
print("=======Corpus Stats=======")
print("Number of elements:", len(corpus))
var min = 100000000
var max = 0
var sum = 0
var count = 0
for i in range(len(corpus)):
var key = corpus[i]
if len(key) == 0:
continue
count += 1
sum += len(key)
if min > len(key):
min = len(key)
if max < len(key):
max = len(key)
var avg = sum / count
print("Min key lenght:", min)
print("Avg key length:", avg)
print("Max key length:", max)
print("Total num of bytes:", sum)
print("\n")
fn main() raises:
var d1 = Dict[Int]()
var d2 = StdDict[StringKey, Int]()
var corpus = french_text_to_keys()
print("")
corpus_stats(corpus)
@parameter
fn build_compact_dict():
var d = Dict[Int](len(corpus))
# var d = Dict[Int]()
for i in range(len(corpus)):
try:
_ = d.put(StringKey(corpus[i]), i)
except:
print("!!!")
d1 = d^
@parameter
fn build_std_dict():
var d = StdDict[StringKey, Int]()
for i in range(len(corpus)):
d[corpus[i]] = i
d2 = d^
print("+++++++Create Dict Benchmark+++++++")
var build_compact_stats = benchmark.run[build_compact_dict](max_runtime_secs=0.5)
# build_compact_stats.print("ns")
var build_std_stats = benchmark.run[build_std_dict](max_runtime_secs=0.5)
# build_std_stats.print("ns")
print("Compact build speedup:", build_std_stats.mean() / build_compact_stats.mean())
var sum1 = 0
@parameter
fn read_compact_dict():
sum1 = 0
for i in range(len(corpus)):
try:
sum1 += d1.get(StringKey(corpus[i]), -1)
except:
print("!!!!!")
# d1.keys.print_keys()
print("+++++++Read Dict Benchmark+++++++")
var read_compact_stats = benchmark.run[read_compact_dict](max_runtime_secs=0.5)
print("Sum1:", sum1, len(d1))
# read_compact_stats.print("ns")
var sum2 = 0
@parameter
fn read_std_dict():
sum2 = 0
for i in range(len(corpus)):
try:
sum2 += d2[corpus[i]]
except:
sum2 += -1
var raed_std_stats = benchmark.run[read_std_dict](max_runtime_secs=0.5)
# raed_std_stats.print("ns")
print("Sum2:", sum2, len(d2))
print("Compact read speedup:", raed_std_stats.mean() / read_compact_stats.mean())
assert_equal(sum1, sum2)
assert_equal(len(d1), len(d2))
var m = 9
@parameter
fn delete_compact_dict():
for i in range(len(corpus)):
if i % m == 0:
try:
d1.delete(StringKey(corpus[i]))
except:
print("!!!!!!!!!!!!!!")
@parameter
fn delete_std_dict():
for i in range(len(corpus)):
if i % m == 0:
try:
_ = d2.pop(corpus[i])
except:
pass
print("+++++++Delete Dict Benchmark+++++++")
var delete_compact_stats = benchmark.run[delete_compact_dict](max_runtime_secs=0.5)
var delete_std_stats = benchmark.run[delete_std_dict](max_runtime_secs=0.5)
print("Compact delete speedup:", delete_std_stats.mean() / delete_compact_stats.mean())
print("+++++++Read After Delete Dict Benchmark+++++++")
var read_after_delete_compact_stats = benchmark.run[read_compact_dict](max_runtime_secs=0.5)
var read_after_delete_std_stats = benchmark.run[read_std_dict](max_runtime_secs=0.5)
print("Compact read after delete speedup:", read_after_delete_std_stats.mean() / read_after_delete_compact_stats.mean())
print("Sum1:", sum1, "length:", len(d1))
print("Sum2:", sum2, "length:", len(d2))
assert_equal(sum1, sum2)
assert_equal(len(d1), len(d2))
_ = corpus
_ = d1^
_ = d2^