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:
J. Nick Koston
2024-01-02 22:13:23 -10:00
committed by GitHub
parent 710e55fb09
commit 7b3ec60f90

View File

@@ -11,7 +11,6 @@ from itertools import chain, groupby
import logging
from operator import itemgetter
import re
from statistics import mean
from typing import TYPE_CHECKING, Any, Literal, TypedDict, cast
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"
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__)