03568c88e3
- frontend/functions/core/format_datetime_short.md - frontend/functions/core/format_datetime_short.test.ts - frontend/functions/core/format_datetime_short.ts - frontend/functions/core/format_duration.md - frontend/functions/core/format_duration.test.ts - frontend/functions/core/format_duration.ts - frontend/functions/core/month_grid.md - frontend/functions/core/month_grid.test.ts - frontend/functions/core/month_grid.ts - frontend/functions/core/string_hash_palette.md - ... Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
28 lines
693 B
Go
28 lines
693 B
Go
package datascience
|
|
|
|
import "sort"
|
|
|
|
// DurationStatsFrom computes descriptive statistics for a slice of durations in milliseconds.
|
|
// It sorts a local copy so the original slice is not mutated.
|
|
// Returns a zero-value DurationStats when the input is empty.
|
|
func DurationStatsFrom(durations []int64) DurationStats {
|
|
n := len(durations)
|
|
if n == 0 {
|
|
return DurationStats{}
|
|
}
|
|
cp := make([]int64, n)
|
|
copy(cp, durations)
|
|
sort.Slice(cp, func(i, j int) bool { return cp[i] < cp[j] })
|
|
var sum int64
|
|
for _, d := range cp {
|
|
sum += d
|
|
}
|
|
return DurationStats{
|
|
N: n,
|
|
AvgMs: sum / int64(n),
|
|
P50Ms: Percentile(cp, 0.5),
|
|
P90Ms: Percentile(cp, 0.9),
|
|
P99Ms: Percentile(cp, 0.99),
|
|
}
|
|
}
|