From 4e9075054f73a6d5c2ab11b29cea078b25a9a8a0 Mon Sep 17 00:00:00 2001 From: Matthew Rocklin Date: Tue, 20 Dec 2022 10:04:04 -0600 Subject: [PATCH] Avoid overflow in statitics.mean MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don't know why, but for some reason statistics.mean was overflowing in CI. See https://github.com/dask/distributed/actions/runs/3741526593/jobs/6351258185 I'm trying a naive implementation instead. It also happens to be faster and simpler. ```python In [1]: from statistics import mean In [2]: x = list(range(1000)) In [3]: %timeit mean(x) 196 µs ± 777 ns per loop (mean ± std. dev. of 7 runs, 1,000 loops each) In [4]: %timeit sum(x) / len(x) 4.82 µs ± 15.2 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each) ``` --- distributed/dashboard/components/shared.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/distributed/dashboard/components/shared.py b/distributed/dashboard/components/shared.py index 957dc0cf9b3..48f3c3df5df 100644 --- a/distributed/dashboard/components/shared.py +++ b/distributed/dashboard/components/shared.py @@ -2,7 +2,6 @@ import asyncio import weakref -from statistics import mean import tlz as toolz from bokeh.core.properties import without_property_validation @@ -559,6 +558,9 @@ def get_data(self): @without_property_validation @log_errors def update(self): + def mean(x): + return sum(x) / len(x) + self.source.stream(self.get_data(), 1000) self.label_source.data["cpu"] = [ "{}: {:.1f}%".format(f.__name__, f(self.source.data["cpu"]))