File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ import (
17
17
"fmt"
18
18
"hash/fnv"
19
19
"math"
20
+ "sort"
20
21
"sync/atomic"
21
22
22
23
"github.com/golang/protobuf/proto"
@@ -231,11 +232,18 @@ func (h *histogram) Desc() *Desc {
231
232
}
232
233
233
234
func (h * histogram ) Observe (v float64 ) {
234
- for i , upperBound := range h .upperBounds {
235
- if v <= upperBound {
236
- atomic .AddUint64 (& h .counts [i ], 1 )
237
- break
238
- }
235
+ // TODO(beorn7): For small numbers of buckets (<30), a linear search is
236
+ // slightly faster than the binary search. If we really care, we could
237
+ // switch from one search strategy to the other depending on the number
238
+ // of buckets.
239
+ //
240
+ // Microbenchmarks (BenchmarkHistogramNoLabels):
241
+ // 11 buckets: 38.3 ns/op linear - binary 48.7 ns/op
242
+ // 100 buckets: 78.1 ns/op linear - binary 54.9 ns/op
243
+ // 300 buckets: 154 ns/op linear - binary 61.6 ns/op
244
+ i := sort .SearchFloat64s (h .upperBounds , v )
245
+ if i < len (h .counts ) {
246
+ atomic .AddUint64 (& h .counts [i ], 1 )
239
247
}
240
248
atomic .AddUint64 (& h .count , 1 )
241
249
for {
You can’t perform that action at this time.
0 commit comments