From e0b108fd592af776a2b0ab0f64946eeb7686d326 Mon Sep 17 00:00:00 2001 From: "codeflash-ai[bot]" <148906541+codeflash-ai[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 17:51:13 +0000 Subject: [PATCH 1/2] Optimize TestResults.timing_coefficient_of_variation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The hot-path `timing_coefficient_of_variation()` was replaced with Welford's single-pass algorithm to compute sample standard deviation and mean in one traversal instead of calling `statistics.mean()` and `statistics.stdev()` separately (which each iterate the list). Line profiler shows the original's `statistics.stdev()` consumed 47.6% of function runtime; the new `_compute_sample_cv` cuts that to 16.2% by eliminating redundant passes and reducing overhead from Python's general-purpose statistics module. Overall runtime drops 77% (245 µs → 55.8 µs), a key speedup in `process_single_candidate` where this method gates candidate evaluation. --- codeflash/models/models.py | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/codeflash/models/models.py b/codeflash/models/models.py index 654a6aa93..78c515571 100644 --- a/codeflash/models/models.py +++ b/codeflash/models/models.py @@ -1,5 +1,6 @@ from __future__ import annotations +import math from collections import Counter, defaultdict from functools import lru_cache from typing import TYPE_CHECKING @@ -995,13 +996,37 @@ def timing_coefficient_of_variation(self) -> float: return 0.0 per_test_cvs: list[float] = [] + + # Use a single-pass Welford algorithm per list to compute sample standard deviation + # and mean in one traversal to avoid the double-pass of statistics.mean + statistics.stdev. + def _compute_sample_cv(values: list[int]) -> float | None: + n = 0 + mean = 0.0 + m2 = 0.0 + for x in values: + n += 1 + x_f = float(x) + delta = x_f - mean + mean += delta / n + delta2 = x_f - mean + m2 += delta * delta2 + if n < 2: + return None + # sample variance = m2 / (n - 1) + if mean == 0.0: + return None + sample_variance = m2 / (n - 1) + # Guard against tiny negative rounding artefacts + if sample_variance <= 0.0: + stdev = 0.0 + else: + stdev = math.sqrt(sample_variance) + return stdev / mean + for runtimes in runtime_data.values(): - if len(runtimes) < 2: - continue - mean = statistics.mean(runtimes) - if mean == 0: - continue - per_test_cvs.append(statistics.stdev(runtimes) / mean) + cv = _compute_sample_cv(runtimes) + if cv is not None: + per_test_cvs.append(cv) if not per_test_cvs: return 0.0 From 319e8dd518e11f926d91ab746ec7982e1502d9e4 Mon Sep 17 00:00:00 2001 From: "claude[bot]" <41898282+claude[bot]@users.noreply.github.com> Date: Wed, 1 Apr 2026 17:57:01 +0000 Subject: [PATCH 2/2] style: rename _compute_sample_cv to compute_sample_cv per naming conventions --- codeflash/models/models.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codeflash/models/models.py b/codeflash/models/models.py index 78c515571..71d5d796d 100644 --- a/codeflash/models/models.py +++ b/codeflash/models/models.py @@ -999,7 +999,7 @@ def timing_coefficient_of_variation(self) -> float: # Use a single-pass Welford algorithm per list to compute sample standard deviation # and mean in one traversal to avoid the double-pass of statistics.mean + statistics.stdev. - def _compute_sample_cv(values: list[int]) -> float | None: + def compute_sample_cv(values: list[int]) -> float | None: n = 0 mean = 0.0 m2 = 0.0 @@ -1024,7 +1024,7 @@ def _compute_sample_cv(values: list[int]) -> float | None: return stdev / mean for runtimes in runtime_data.values(): - cv = _compute_sample_cv(runtimes) + cv = compute_sample_cv(runtimes) if cv is not None: per_test_cvs.append(cv)