From f822d1acc16281471a3b8ea0f0fbb0b64144e106 Mon Sep 17 00:00:00 2001 From: Anthony Shaw Date: Thu, 3 Mar 2022 08:26:43 +1100 Subject: [PATCH] Add more benchmarks --- .vscode/settings.json | 4 ++-- bench_class.py | 47 +++++++++++++++++++++++++++++++++++++++++++ bench_functions.py | 11 +++------- 3 files changed, 52 insertions(+), 10 deletions(-) create mode 100644 bench_class.py diff --git a/.vscode/settings.json b/.vscode/settings.json index 005d289..05d17da 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "python.linting.banditEnabled": false, - "python.linting.pylintEnabled": true, - "python.linting.enabled": true + "python.linting.pylintEnabled": false, + "python.linting.enabled": false } \ No newline at end of file diff --git a/bench_class.py b/bench_class.py new file mode 100644 index 0000000..f64e08f --- /dev/null +++ b/bench_class.py @@ -0,0 +1,47 @@ +from collections import namedtuple +from dataclasses import dataclass + + +def attributes_in_class(): + class Pet: + legs: int + noise: str + + def __init__(self, legs, noise) -> None: + self.legs = legs + self.noise = noise + + def __repr__(self): + return f"" + + for _ in range(1000): + dog = Pet(4, "woof") + str(dog) + +def attributes_in_dataclass(): + @dataclass + class Pet: + legs: int + noise: str + + for _ in range(1000): + dog = Pet(4, "woof") + str(dog) + +def attributes_in_namedtuple(): + Pet = namedtuple("Pet", "legs noise") + for _ in range(1000): + dog = Pet(4, "woof") + str(dog) + +def attributes_in_dict(): + for _ in range(1000): + dog = {"legs": 4, "noise": "woof"} + str(dog) + +__benchmarks__ = [ + (attributes_in_dataclass, attributes_in_class, "Class instead of dataclass"), + (attributes_in_dataclass, attributes_in_namedtuple, "Namedtuple instead of dataclass"), + (attributes_in_namedtuple, attributes_in_class, "class instead of namedtuple"), + (attributes_in_class, attributes_in_dict, "dict instead of class"), +] \ No newline at end of file diff --git a/bench_functions.py b/bench_functions.py index da9164b..a3bcbe8 100644 --- a/bench_functions.py +++ b/bench_functions.py @@ -6,17 +6,12 @@ def func_with_named_args(a, b, c): def keyword_call(): func_with_kwargs(a=1, b=2, c=3) - func_with_kwargs(a=1, b=2, c=3) - func_with_kwargs(a=1, b=2, c=3) - func_with_kwargs(a=1, b=2, c=3) - func_with_kwargs(a=1, b=2, c=3) + def positional_call(): + numbers = (1, 2, 3) func_with_named_args(a=1, b=2, c=3) - func_with_named_args(a=1, b=2, c=3) - func_with_named_args(a=1, b=2, c=3) - func_with_named_args(a=1, b=2, c=3) - func_with_named_args(a=1, b=2, c=3) + def tiny_func(x, y):