diff options
author | Jim Mussared <jim.mussared@gmail.com> | 2023-10-13 14:47:59 +1100 |
---|---|---|
committer | Jim Mussared <jim.mussared@gmail.com> | 2023-10-27 16:16:08 +1100 |
commit | 1a017511d0b25b8b4a6c3aaa2f2bc17e06b32fa2 (patch) | |
tree | be34f78a9460a874cc49821605df692a0a1f23d5 /tests/perf_bench/core_str.py | |
parent | 2fda94c28629f7c12dcab931b2bf4cc0f9c8f734 (diff) | |
download | micropython-1a017511d0b25b8b4a6c3aaa2f2bc17e06b32fa2.tar.gz micropython-1a017511d0b25b8b4a6c3aaa2f2bc17e06b32fa2.zip |
tests/perf_bench: Add string/qstr/map tests.
These tests are designed to measure changes in performance relating to:
- string interning / searching for existing strings
- map lookup
- string operations
- string hashing
This work was funded through GitHub Sponsors.
Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Diffstat (limited to 'tests/perf_bench/core_str.py')
-rw-r--r-- | tests/perf_bench/core_str.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/tests/perf_bench/core_str.py b/tests/perf_bench/core_str.py new file mode 100644 index 0000000000..ca827194d6 --- /dev/null +++ b/tests/perf_bench/core_str.py @@ -0,0 +1,65 @@ +# This tests string handling operations + +ZEN = """ +The Zen of Python +Beautiful is better than ugly. +Explicit is better than implicit. +Simple is better than complex. +Complex is better than complicated. +Flat is better than nested. +Sparse is better than dense. +Readability counts. +Special cases aren't special enough to break the rules. +Although practicality beats purity. +Errors should never pass silently. +Unless explicitly silenced. +In the face of ambiguity, refuse the temptation to guess. +There should be one-- and preferably only one --obvious way to do it. +Although that way may not be obvious at first unless you're Dutch. +Now is better than never. +Although never is often better than *right* now. +If the implementation is hard to explain, it's a bad idea. +If the implementation is easy to explain, it may be a good idea. +Namespaces are one honking great idea -- let's do more of those! +""" + + +def test(niter): + counts = {} + for _ in range(niter): + x = ZEN.replace("\n", " ").split(" ") + y = " ".join(x) + for i in range(50): + a = ZEN[i : i * 2] + b = a + "hello world" + for c in ZEN: + i = ord(c) + c = chr(i) + return (x[0], a) + + +########################################################################### +# Benchmark interface + +bm_params = { + (32, 10): (2,), + (50, 10): (3,), + (100, 10): (6,), + (500, 10): (30,), + (1000, 10): (60,), + (5000, 10): (300,), +} + + +def bm_setup(params): + (niter,) = params + state = None + + def run(): + nonlocal state + state = test(niter) + + def result(): + return niter, state + + return run, result |