Speed up getting the mean of statistics (#106930)
All the values we need to get the mean for are always list[float] so we can use a much simpler algorithm to get the mean of the list
This commit is contained in:
parent
710e55fb09
commit
7b3ec60f90
1 changed files with 11 additions and 1 deletions
|
@ -11,7 +11,6 @@ from itertools import chain, groupby
|
||||||
import logging
|
import logging
|
||||||
from operator import itemgetter
|
from operator import itemgetter
|
||||||
import re
|
import re
|
||||||
from statistics import mean
|
|
||||||
from typing import TYPE_CHECKING, Any, Literal, TypedDict, cast
|
from typing import TYPE_CHECKING, Any, Literal, TypedDict, cast
|
||||||
|
|
||||||
from sqlalchemy import Select, and_, bindparam, func, lambda_stmt, select, text
|
from sqlalchemy import Select, and_, bindparam, func, lambda_stmt, select, text
|
||||||
|
@ -145,6 +144,17 @@ STATISTIC_UNIT_TO_UNIT_CONVERTER: dict[str | None, type[BaseUnitConverter]] = {
|
||||||
DATA_SHORT_TERM_STATISTICS_RUN_CACHE = "recorder_short_term_statistics_run_cache"
|
DATA_SHORT_TERM_STATISTICS_RUN_CACHE = "recorder_short_term_statistics_run_cache"
|
||||||
|
|
||||||
|
|
||||||
|
def mean(values: list[float]) -> float | None:
|
||||||
|
"""Return the mean of the values.
|
||||||
|
|
||||||
|
This is a very simple version that only works
|
||||||
|
with a non-empty list of floats. The built-in
|
||||||
|
statistics.mean is more robust but is is almost
|
||||||
|
an order of magnitude slower.
|
||||||
|
"""
|
||||||
|
return sum(values) / len(values)
|
||||||
|
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue