← back

Calculate P50/P95/P99 Latency Percentiles

#293 · Statistics · Easy

⊣ Solve on deep-ml.com

Problem

Calculate P50, P95, and P99 latency percentiles from a list of response times. Percentiles are critical for understanding the distribution of latencies in production systems.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import numpy as np

def calculate_percentiles(latencies: list[float]) -> dict:
    latencies = np.array(latencies)
    p50 = float(np.percentile(latencies, 50))
    p95 = float(np.percentile(latencies, 95))
    p99 = float(np.percentile(latencies, 99))
    return {"p50": round(p50, 2), "p95": round(p95, 2), "p99": round(p99, 2)}

def percentile_from_scratch(latencies: list[float], p: float) -> float:
    sorted_lat = sorted(latencies)
    n = len(sorted_lat)
    rank = (p / 100) * (n - 1)
    lower = int(rank)
    upper = min(lower + 1, n - 1)
    frac = rank - lower
    return sorted_lat[lower] + frac * (sorted_lat[upper] - sorted_lat[lower])

Explanation

  1. Sort the latency values in ascending order.
  2. Compute the fractional rank for the desired percentile: rank = (p/100) * (n-1).
  3. Interpolate linearly between the two nearest values to get the precise percentile.
  4. P50 (median) shows typical latency, P95 shows the latency for the slowest 5% of requests, and P99 captures tail latency affecting 1% of users.

Complexity

  • Time: O(n log n) for sorting
  • Space: O(n) for the sorted copy