Skip to content

Commit

Permalink
tests/perf_bench: Add string/qstr/map tests.
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jimmo committed Oct 27, 2023
1 parent 2fda94c commit 1a01751
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/perf_bench/bm_wordcount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# This tests using string as dictionary keys when they are not qstrs

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 arent 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 youre dutch now is better than never although never is often better than right now if the implementation is hard to explain its a bad idea if the implementation is easy to explain it may be a good idea namespaces are one honking great idea lets do more of those"


def test(niter):
words = ZEN.split(" ")
for _ in range(niter):
counts = {}
for _ in range(niter):
for word in words:
counts[word] = counts.get(word, 0) + 1

return (
counts["python"],
counts["is"],
counts["than"],
)


###########################################################################
# Benchmark interface

bm_params = {
(32, 10): (2,),
(50, 10): (4,),
(100, 10): (8,),
(500, 10): (40,),
(1000, 10): (80,),
(5000, 10): (400,),
}


def bm_setup(params):
(niter,) = params
state = None

def run():
nonlocal state
state = test(niter)

def result():
return niter, state

return run, result
191 changes: 191 additions & 0 deletions tests/perf_bench/core_locals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# This tests the performance of an instance class locals dict (importantly, that has all keys as qstrs)

# These are all shorter than 10 characters, so will be interned by the parser.
ZEN = [
"the",
"zen",
"of",
"python",
"beautiful",
"is",
"better",
"than",
"ugly",
"explicit",
"is",
"better",
"than",
"implicit",
"simple",
"is",
"better",
"than",
"complex",
"complex",
"is",
"better",
"than",
"complicate",
"flat",
"is",
"better",
"than",
"nested",
"sparse",
"is",
"better",
"than",
"dense",
"readabilit",
"counts",
"special",
"cases",
"arent",
"special",
"enough",
"to",
"break",
"the",
"rules",
"although",
"practicali",
"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",
"youre",
"dutch",
"now",
"is",
"better",
"than",
"never",
"although",
"never",
"is",
"often",
"better",
"than",
"right",
"now",
"if",
"the",
"implementa",
"is",
"hard",
"to",
"explain",
"its",
"a",
"bad",
"idea",
"if",
"the",
"implementa",
"is",
"easy",
"to",
"explain",
"it",
"may",
"be",
"a",
"good",
"idea",
"namespaces",
"are",
"one",
"honking",
"great",
"idea",
"",
"lets",
"do",
"more",
"of",
"those",
]


class A:
pass


def test(niter):
for _ in range(niter):
a = A()
for _ in range(niter):
for word in ZEN:
setattr(a, word, getattr(a, word, 0) + 1)

return (
getattr(a, "python"),
getattr(a, "is"),
getattr(a, "than"),
)


###########################################################################
# Benchmark interface

bm_params = {
(32, 10): (2,),
(50, 10): (4,),
(100, 10): (8,),
(500, 10): (40,),
(1000, 10): (80,),
(5000, 10): (400,),
}


def bm_setup(params):
(niter,) = params
state = None

def run():
nonlocal state
state = test(niter)

def result():
return niter, state

return run, result
65 changes: 65 additions & 0 deletions tests/perf_bench/core_str.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 1a01751

Please sign in to comment.