Computes a percentile from a list of floats using linear interpolation, returning 0 for empty input. Useful for summarizing benchmark result distributions.
# Source: https://github.com/Shaivpidadi/FreeRideV3 | License: MIT | Copyright 2026 Shaishav Pidadi
# Provided via justclaw.it.com; keep this notice with the code.
def _percentile(values: list[float], p: float) -> float:
"""Linear-interpolation percentile. Returns 0 for empty input."""
if not values:
return 0.0
s = sorted(values)
if len(s) == 1:
return s[0]
k = (len(s) - 1) * p / 100.0
f = int(k)
c = min(f + 1, len(s) - 1)
if f == c:
return s[f]
return s[f] + (s[c] - s[f]) * (k - f)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key