From 480677b0c97b8fdb3a3a35917e945f1a14c56cd8 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Fri, 15 Jan 2021 11:04:57 -0800 Subject: [PATCH] Add benchmark reading instance variables defined by __slots__ --- .../benchmarks/bm_read_instancevar_slots.py | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 pyperformance/benchmarks/bm_read_instancevar_slots.py diff --git a/pyperformance/benchmarks/bm_read_instancevar_slots.py b/pyperformance/benchmarks/bm_read_instancevar_slots.py new file mode 100644 index 00000000..de42784e --- /dev/null +++ b/pyperformance/benchmarks/bm_read_instancevar_slots.py @@ -0,0 +1,36 @@ +""" +Exercise reading instance members defined by __slots__. +""" +import pyperf + + +REPS = 100000 + + +class Point: + __slots__ = ("a", "b", "c") + + def __init__(self, a=0, b=0, c=0): + self.a = a + self.b = b + self.c = c + + +def read_vars(p): + return p.a + p.b + p.c + + +def benchmark(n): + p = Point() + assert read_vars(p) == 0 + for i in range(n): + read_vars(p) + assert read_vars(p) == 0 + + +if __name__ == "__main__": + runner = pyperf.Runner() + runner.metadata['description'] = "Read __slots__ benchmark" + + reps = REPS + runner.bench_func('read_instancevar_slots', benchmark, reps)