Skip to content

Commit 3a97688

Browse files
sjanelgjasny
authored andcommitted
feat(core): binary search for bucket boundaries in Histogram::Observe
(cherry picked from commit 4d66807)
1 parent b2d942c commit 3a97688

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

core/src/histogram.cc

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@ Histogram::Histogram(const BucketBoundaries& buckets)
3030
}
3131

3232
void Histogram::Observe(const double value) {
33-
// TODO: determine bucket list size at which binary search would be faster
34-
const auto bucket_index = static_cast<std::size_t>(std::distance(
35-
bucket_boundaries_.begin(),
36-
std::find_if(
37-
std::begin(bucket_boundaries_), std::end(bucket_boundaries_),
38-
[value](const double boundary) { return boundary >= value; })));
33+
const auto bucket_index = static_cast<std::size_t>(
34+
std::distance(bucket_boundaries_.begin(),
35+
std::lower_bound(bucket_boundaries_.begin(),
36+
bucket_boundaries_.end(), value)));
3937

4038
std::lock_guard<std::mutex> lock(mutex_);
4139
sum_.Increment(value);

0 commit comments

Comments
 (0)