From b2360315540d2585f2618f3e05fe535bae73fca3 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Fri, 14 Jun 2024 13:57:12 +0800 Subject: [PATCH] add benchmark --- benchmarks/dict_0.py | 20 ++++++++++++++++++++ benchmarks/dict_1.py | 27 +++++++++++++++++++++++++++ build_g.sh | 2 ++ 3 files changed, 49 insertions(+) create mode 100644 benchmarks/dict_0.py create mode 100644 benchmarks/dict_1.py diff --git a/benchmarks/dict_0.py b/benchmarks/dict_0.py new file mode 100644 index 00000000..9637fac5 --- /dev/null +++ b/benchmarks/dict_0.py @@ -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 + diff --git a/benchmarks/dict_1.py b/benchmarks/dict_1.py new file mode 100644 index 00000000..6c5daa31 --- /dev/null +++ b/benchmarks/dict_1.py @@ -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 diff --git a/build_g.sh b/build_g.sh index bffbb51f..5744fb07 100644 --- a/build_g.sh +++ b/build_g.sh @@ -1,3 +1,5 @@ +set -e + python prebuild.py SRC_C=$(find src/ -name "*.c")