mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-19 19:10:17 +00:00
add benchmark for dict
This commit is contained in:
parent
907a1b7713
commit
0770c97f60
20
benchmarks/dict_0.py
Normal file
20
benchmarks/dict_0.py
Normal 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
27
benchmarks/dict_1.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user