mirror of
https://github.com/featurebasedb/featurebase.git
synced 2026-09-06 16:45:55 +00:00
Move sqrt out of Add()
This commit is contained in:
parent
51faa59ada
commit
75d282a478
1 changed files with 6 additions and 10 deletions
|
|
@ -9,9 +9,7 @@ type Stats struct {
|
|||
Min time.Duration
|
||||
Max time.Duration
|
||||
Mean time.Duration
|
||||
SumSquareDelta float64
|
||||
Variance float64
|
||||
StdDev time.Duration
|
||||
sumSquareDelta float64
|
||||
Total time.Duration
|
||||
Num int64
|
||||
}
|
||||
|
|
@ -32,15 +30,11 @@ func (s *Stats) Add(td time.Duration) {
|
|||
s.Max = td
|
||||
}
|
||||
|
||||
// these three comprise an online variance calculation
|
||||
// online variance calculation
|
||||
// https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Online_algorithm
|
||||
delta := td - s.Mean
|
||||
s.Mean += delta / time.Duration(s.Num)
|
||||
s.SumSquareDelta += float64(delta * (td - s.Mean))
|
||||
|
||||
// these are the useful results, but don't need to be updated every iteration
|
||||
s.Variance = s.SumSquareDelta / float64(s.Num)
|
||||
s.StdDev = time.Duration(math.Sqrt(s.Variance))
|
||||
s.sumSquareDelta += float64(delta * (td - s.Mean))
|
||||
}
|
||||
|
||||
func (s *Stats) Avg() time.Duration {
|
||||
|
|
@ -50,5 +44,7 @@ func (s *Stats) Avg() time.Duration {
|
|||
func AddToResults(s *Stats, results map[string]interface{}) {
|
||||
results["min"] = s.Min
|
||||
results["max"] = s.Max
|
||||
results["avg"] = s.Avg()
|
||||
results["avg"] = s.Mean
|
||||
variance := s.sumSquareDelta / float64(s.Num)
|
||||
results["std"] = time.Duration(math.Sqrt(variance))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue