示例#1
0
func Analyze(times []float64) {
	sorted := sort.Float64Array(times)
	sorted.Sort()

	tot := 0.0
	for _, v := range times {
		tot += v
	}
	n := float64(len(times))

	avg := tot / n
	variance := 0.0
	for _, v := range times {
		variance += (v - avg) * (v - avg)
	}
	variance /= n

	stddev := math.Sqrt(variance)

	median := sorted[len(times)/2]
	perc90 := sorted[int(n*0.9)]
	perc10 := sorted[int(n*0.1)]

	fmt.Printf(
		"%d samples\n"+
			"avg %.2f ms 2sigma %.2f "+
			"median %.2fms\n"+
			"10%%tile %.2fms, 90%%tile %.2fms\n",
		len(times), avg, 2*stddev, median, perc10, perc90)
}
示例#2
0
文件: stat.go 项目: vdobler/ft
// Compute minimum, p percentil, median, average, 100-p percentil and maximum of values in data.
func SixvalFloat64(data []float64, p int) (min, lq, med, avg, uq, max float64) {
	n := len(data)

	// Special cases 0 and 1
	if n == 0 {
		return
	}

	if n == 1 {
		min = data[0]
		lq = data[0]
		med = data[0]
		avg = data[0]
		uq = data[0]
		max = data[0]
		return
	}

	// First pass (min, max, coarse average)
	var sum float64
	min, max = math.MaxFloat64, -math.MaxFloat64
	for _, v := range data {
		if v < min {
			min = v
		}
		if v > max {
			max = v
		}
		sum += v
	}
	avg = sum / float64(n)

	// Second pass: Correct average
	var corr float64
	for _, v := range data {
		corr += v - avg
	}
	avg += corr / float64(n)

	// Median
	sort.Sort(sort.Float64Array(data))
	if n%2 == 1 {
		med = data[(n-1)/2]
	} else {
		med = (data[n/2] + data[n/2-1]) / 2
	}

	// Percentiles
	if p < 0 {
		p = 0
	}
	if p > 100 {
		p = 100
	}
	lq = percentilFloat64(data, p)
	uq = percentilFloat64(data, 100-p)
	return
}