Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011 The LevelDB Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file. See the AUTHORS file for names of contributors.5#include "util/histogram.h"7#include <math.h>8#include <stdio.h>10#include "port/port.h"12namespace leveldb {14const double Histogram::kBucketLimit[kNumBuckets] = {151,162,173,184,195,206,217,228,239,2410,2512,2614,2716,2818,2920,3025,3130,3235,3340,3445,3550,3660,3770,3880,3990,40100,41120,42140,43160,44180,45200,46250,47300,48350,49400,50450,51500,52600,53700,54800,55900,561000,571200,581400,591600,601800,612000,622500,633000,643500,654000,664500,675000,686000,697000,708000,719000,7210000,7312000,7414000,7516000,7618000,7720000,7825000,7930000,8035000,8140000,8245000,8350000,8460000,8570000,8680000,8790000,88100000,89120000,90140000,91160000,92180000,93200000,94250000,95300000,96350000,97400000,98450000,99500000,100600000,101700000,102800000,103900000,1041000000,1051200000,1061400000,1071600000,1081800000,1092000000,1102500000,1113000000,1123500000,1134000000,1144500000,1155000000,1166000000,1177000000,1188000000,1199000000,12010000000,12112000000,12214000000,12316000000,12418000000,12520000000,12625000000,12730000000,12835000000,12940000000,13045000000,13150000000,13260000000,13370000000,13480000000,13590000000,136100000000,137120000000,138140000000,139160000000,140180000000,141200000000,142250000000,143300000000,144350000000,145400000000,146450000000,147500000000,148600000000,149700000000,150800000000,151900000000,1521000000000,1531200000000,1541400000000,1551600000000,1561800000000,1572000000000,1582500000000.0,1593000000000.0,1603500000000.0,1614000000000.0,1624500000000.0,1635000000000.0,1646000000000.0,1657000000000.0,1668000000000.0,1679000000000.0,1681e200,169};171void Histogram::Clear() {172min_ = kBucketLimit[kNumBuckets - 1];173max_ = 0;174num_ = 0;175sum_ = 0;176sum_squares_ = 0;177for (int i = 0; i < kNumBuckets; i++) {178buckets_[i] = 0;179}180}182void Histogram::Add(double value) {183// Linear search is fast enough for our usage in db_bench184int b = 0;185while (b < kNumBuckets - 1 && kBucketLimit[b] <= value) {186b++;187}188buckets_[b] += 1.0;189if (min_ > value) min_ = value;190if (max_ < value) max_ = value;191num_++;192sum_ += value;193sum_squares_ += (value * value);194}196void Histogram::Merge(const Histogram& other) {197if (other.min_ < min_) min_ = other.min_;198if (other.max_ > max_) max_ = other.max_;199num_ += other.num_;200sum_ += other.sum_;201sum_squares_ += other.sum_squares_;202for (int b = 0; b < kNumBuckets; b++) {203buckets_[b] += other.buckets_[b];204}205}207double Histogram::Median() const { return Percentile(50.0); }209double Histogram::Percentile(double p) const {210double threshold = num_ * (p / 100.0);211double sum = 0;212for (int b = 0; b < kNumBuckets; b++) {213sum += buckets_[b];214if (sum >= threshold) {215// Scale linearly within this bucket216double left_point = (b == 0) ? 0 : kBucketLimit[b - 1];217double right_point = kBucketLimit[b];218double left_sum = sum - buckets_[b];219double right_sum = sum;220double pos = (threshold - left_sum) / (right_sum - left_sum);221double r = left_point + (right_point - left_point) * pos;222if (r < min_) r = min_;223if (r > max_) r = max_;224return r;225}226}227return max_;228}230double Histogram::Average() const {231if (num_ == 0.0) return 0;232return sum_ / num_;233}235double Histogram::StandardDeviation() const {236if (num_ == 0.0) return 0;237double variance = (sum_squares_ * num_ - sum_ * sum_) / (num_ * num_);238return sqrt(variance);239}241std::string Histogram::ToString() const {242std::string r;243char buf[200];244snprintf(buf, sizeof(buf), "Count: %.0f Average: %.4f StdDev: %.2f\n", num_,245Average(), StandardDeviation());246r.append(buf);247snprintf(buf, sizeof(buf), "Min: %.4f Median: %.4f Max: %.4f\n",248(num_ == 0.0 ? 0.0 : min_), Median(), max_);249r.append(buf);250r.append("------------------------------------------------------\n");
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.