add benchmark for dict

This commit is contained in:
blueloveTH 2024-06-14 14:01:25 +08:00
parent 907a1b7713
commit 0770c97f60
2 changed files with 47 additions and 0 deletions

20
benchmarks/dict_0.py Normal file
View File

@ -0,0 +1,20 @@
# test basic get/set
import random
random.seed(7)
a = {str(i): i for i in range(100)}
a['existed'] = 0
a['missed'] = 0
for i in range(1000000):
key = str(random.randint(-100, 100))
if key in a:
a['existed'] += 1
else:
a['missed'] += 1
existed = a['existed']
missed = a['missed']
assert abs(existed - missed) < 10000

27
benchmarks/dict_1.py Normal file
View File

@ -0,0 +1,27 @@
# test deletion
rnd = 0
keys = []
while True:
keys.append(rnd)
rnd = ((rnd * 5) + 1) & 1023
if rnd == 0:
break
assert len(keys) == 1024
a = {k: k for k in keys}
for i in range(10000):
if i % 2 == 0:
# del all keys
for k in keys:
del a[k]
assert len(a) == 0
else:
# add keys back
for k in keys:
a[k] = k
assert len(a) == len(keys)
assert len(a) == len(keys)
assert list(a.keys()) == keys # order matters