Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvements #9

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions benchmarking/bench_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
from quantio import Time


def init_only_base_unit():
Time(seconds=1)


def bench_init(benchmark):
benchmark.pedantic(init_only_base_unit, iterations=100, rounds=100)
f = lambda: Time(seconds=1, milliseconds=1)
benchmark.pedantic(f, iterations=100, rounds=100)


if __name__ == "__main__":
Expand Down
44 changes: 44 additions & 0 deletions benchmarking/bench_vector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import pytest
import numpy as np

from quantio import Length, Vector


def bench_from_numpy__base_unit(benchmark):
f = lambda: Vector.from_numpy(np.ones(100), Length, "meters")
benchmark.pedantic(f, iterations=100, rounds=100)


def bench_from_numpy__other_unit(benchmark):
f = lambda: Vector.from_numpy(np.ones(100), Length, "centimeters")
benchmark.pedantic(f, iterations=100, rounds=100)


def bench_to_numpy__base_unit(benchmark):
vector = Vector.from_numpy(np.ones(100), Length, "meters")
f = lambda: vector.to_numpy("meters")
benchmark.pedantic(f, iterations=100, rounds=100)


def bench_to_numpy__other_unit(benchmark):
vector = Vector.from_numpy(np.ones(100), Length, "meters")
f = lambda: vector.to_numpy("centimeters")
benchmark.pedantic(f, iterations=100, rounds=100)


def bench_add(benchmark):
vector1 = Vector.from_numpy(np.ones(100), Length, "meters")
vector2 = Vector.from_numpy(np.ones(100), Length, "meters")
f = lambda: vector1 + vector2
benchmark.pedantic(f, iterations=100, rounds=100)


def bench_sub(benchmark):
vector1 = Vector.from_numpy(np.ones(100), Length, "meters")
vector2 = Vector.from_numpy(np.ones(100), Length, "meters")
f = lambda: vector1 - vector2
benchmark.pedantic(f, iterations=100, rounds=100)


if __name__ == "__main__":
pytest.main([__file__, "-v"])
43 changes: 27 additions & 16 deletions generate/generate_boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,50 +67,61 @@ def _generate_properties(current_class: str, units: dict[str, str]) -> list[str]
code = []

for unit, factor in units.items():
code.append(" " * 4 + "@property")
code.append(" " * 4 + f"def {unit}(self) -> float:")
code.append(" " * 8 + f'"""The {current_class.lower()} in {unit.replace("_", " ")}."""')
code.append(" " * 8 + f"return self._base_value / {factor}")
code.append(_indent("@property", 1))
code.append(_indent(f"def {unit}(self) -> float:", 1))
code.append(_indent(f'"""The {current_class.lower()} in {unit.replace("_", " ")}."""', 2))
code.append(_indent(f"return self._base_value / {factor}", 2))
code.append("")

return code


def _generate_init(units: dict[str, str]) -> list[str]:
code = [" " * 4 + "def __init__(", " " * 8 + "self,", " " * 8 + "_base_value: float = 0.0,"]
code = [
_indent("def __init__(", 1),
_indent("self,", 2),
_indent("_base_value: float = 0.0,", 2),
]

for unit in units:
code.append(" " * 8 + f"{unit}: float = 0.0,")
code.append(_indent(f"{unit}: float | None = None,", 2))

code.append(" " * 4 + ") -> None:")
code.append(" " * 8 + "self._base_value = _base_value")
code.append(_indent(") -> None:", 1))
code.append(_indent("self._base_value = _base_value", 2))

for unit, factor in units.items():
code.append(" " * 8 + f"self._base_value += {unit} * {factor}")
code.append(_indent(f"if {unit} is not None:", 2))
code.append(_indent(f"self._base_value += {unit} * {factor}", 3))

code.append("")
return code


def _generat_zero_function(current_class: str) -> list[str]:
return [
" " * 4 + "@classmethod",
" " * 4 + f"def zero(cls) -> {current_class}:",
" " * 8 + f'"""Create a {current_class} with a value of zero."""',
" " * 8 + f"return {current_class}()",
_indent("@classmethod", 1),
_indent(f"def zero(cls) -> {current_class}:", 1),
_indent(f'"""Create a {current_class} with a value of zero."""', 2),
_indent(f"return {current_class}()", 2),
"",
]


def _generat_str_function(current_class: str) -> list[str]:
return [
" " * 4 + f"def __str__(self) -> str:",
" " * 8 + f'"""Display this quantity as a string for printing."""',
" " * 8 + f'return "{current_class}(" + self.BASE_UNIT + "=" + str(self._base_value) + ")"',
_indent(f"def __str__(self) -> str:", 1),
_indent(f'"""Display this quantity as a string for printing."""', 2),
_indent(
f'return "{current_class}(" + self.BASE_UNIT + "=" + str(self._base_value) + ")"', 2
),
"",
]


def _indent(text: str, number_of_indents: int) -> str:
return " " * 4 * number_of_indents + text


if __name__ == "__main__":
quantities_with_fields = {
"Acceleration": {
Expand Down
Loading
Loading