diff --git a/bench_class.py b/bench_class.py index f64e08f..0520863 100644 --- a/bench_class.py +++ b/bench_class.py @@ -1,5 +1,6 @@ from collections import namedtuple from dataclasses import dataclass +import typing def attributes_in_class(): @@ -14,7 +15,24 @@ def attributes_in_class(): def __repr__(self): return f"" - for _ in range(1000): + for _ in range(100000): + dog = Pet(4, "woof") + str(dog) + +def attributes_in_class_with_slots(): + class Pet: + legs: int + noise: str + __slots__ = 'legs', 'noise' + + def __init__(self, legs, noise) -> None: + self.legs = legs + self.noise = noise + + def __repr__(self): + return f"" + + for _ in range(100000): dog = Pet(4, "woof") str(dog) @@ -24,24 +42,46 @@ def attributes_in_dataclass(): legs: int noise: str - for _ in range(1000): + for _ in range(100000): + dog = Pet(4, "woof") + str(dog) + +def attributes_in_dataclass_with_slots(): + @dataclass(slots=True) + class Pet: + legs: int + noise: str + + for _ in range(100000): dog = Pet(4, "woof") str(dog) def attributes_in_namedtuple(): Pet = namedtuple("Pet", "legs noise") - for _ in range(1000): + for _ in range(100000): + dog = Pet(4, "woof") + str(dog) + +def attributes_in_namedtuple_type(): + class Pet(typing.NamedTuple): + legs: int + noise: str + + for _ in range(100000): dog = Pet(4, "woof") str(dog) def attributes_in_dict(): - for _ in range(1000): + for _ in range(100000): dog = {"legs": 4, "noise": "woof"} str(dog) __benchmarks__ = [ (attributes_in_dataclass, attributes_in_class, "Class instead of dataclass"), + (attributes_in_dataclass, attributes_in_dataclass_with_slots, "dataclass with slots"), (attributes_in_dataclass, attributes_in_namedtuple, "Namedtuple instead of dataclass"), (attributes_in_namedtuple, attributes_in_class, "class instead of namedtuple"), + (attributes_in_namedtuple, attributes_in_namedtuple_type, "namedtuple class instead of namedtuple"), (attributes_in_class, attributes_in_dict, "dict instead of class"), -] \ No newline at end of file + (attributes_in_class, attributes_in_class_with_slots, "class with slots"), +]