-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlevel_dict_benchmark.py
112 lines (89 loc) Β· 2.14 KB
/
level_dict_benchmark.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
# stdlib
import json
import time
# this package
from cawdrey.level_dict import LevelDict
def benchmark(dict_class="dict"):
if dict_class == "level":
data = LevelDict("test_dict.ld")
else:
data = dict()
start_time = time.time()
# Add 150000 keys to the dictionary.
for i in range(50000):
data["cat" + str(i)] = str(i)
data["ca" + str(i)] = str(i)
data['c' + str(i)] = str(i)
# Add tested key last.
data["cat"] = "100"
print(f"{time.time() - start_time:f}")
start_time = time.time()
# Loop up the key that was added last to the dictionary.
for i in range(1000):
if "cat" not in data:
break
print(f"{time.time() - start_time:f}")
start_time = time.time()
if dict_class == "dict":
with open("test_dict.json", 'w') as fp:
json.dump(data, fp)
del data
with open("test_dict.json") as fp:
data = json.load(fp)
elif dict_class == "level":
data.db.close()
data = LevelDict("test_dict.ld")
# Loop up the key that was added last to the dictionary.
for i in range(1000):
if "cat" not in data:
break
print(f"{time.time() - start_time:f}")
#
# if dict_class == "level":
# data = LevelDict("test_dict.ld")
# else:
# data = dict()
#
# # Add tested key first.
# data["cat"] = 100
#
# # Add 150000 keys to the dictionary.
# for i in range(50000):
# data["cat" + str(i)] = i
# data["ca" + str(i)] = i
# data["c" + str(i)] = i
#
# start_time = time.time()
#
# # Loop up the key that was added first to the dictionary.
# for i in range(10000000):
# if "cat" not in data:
# break
#
# print(f"{time.time() - start_time:f}")
# print("Normal Dict")
# benchmark("dict")
#
# print("LevelDict")
# benchmark("level")
#
# data = LevelDict("test_dict.ld")
# data["foo"] = "bar"
# assert "foo" in data
#
# print("JSON")
# start_time = time.time()
# for i in range(10000000):
# json.loads("123.456")
# print(f"{time.time() - start_time:f}")
#
# print("Casting")
# start_time = time.time()
# for i in range(10000000):
# float("123.456")
# print(f"{time.time() - start_time:f}")
# this package
from cawdrey.level_dict import TypedLevelDict
class MyDict(TypedLevelDict[str, int]):
key_type = str
value_type = int