Skip to content

Commit a7cc607

Browse files
committed
Better examples
1 parent 285d11a commit a7cc607

2 files changed

Lines changed: 74 additions & 60 deletions

File tree

libs/MicroStats.h

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ inline uint32_t msb(uint64_t value) {
3232
template <uint32_t NDB>
3333
class MicroStats {
3434
public:
35+
//! The number of available bins possible in a 64-bit range
36+
static constexpr uint32_t NUMBINS = (64 - NDB) << NDB;
37+
38+
//! Mask used extensively in the calculations
39+
static constexpr uint64_t MASK = (1ULL << NDB) - 1;
40+
3541
//! Default constructor initializes the structures with proper ranges
3642
MicroStats() {
3743
init();
@@ -95,24 +101,6 @@ class MicroStats {
95101
return out;
96102
}
97103

98-
private:
99-
//! Initializes the histogram with the calculated ranges
100-
void init() {
101-
for (uint32_t j = 0; j < bins.size(); ++j) {
102-
Bin& bin(bins[j]);
103-
bin.clear();
104-
bin.range = calcrange(j);
105-
}
106-
totalcount = 0;
107-
totalsum = 0;
108-
}
109-
110-
//! The number of available bins possible in a 64-bit range
111-
static constexpr uint32_t NUMBINS = (64 - NDB) << NDB;
112-
113-
//! Mask used extensively in the calculations
114-
static constexpr uint64_t MASK = (1ULL << NDB) - 1;
115-
116104
//! Computes the respective bin given a value
117105
static uint32_t calcbin(uint64_t value) {
118106
if (value < (1 << NDB)) return value;
@@ -136,6 +124,18 @@ class MicroStats {
136124
return Range{base + offset, base + (base >> NDB) + offset - 1};
137125
}
138126

127+
private:
128+
//! Initializes the histogram with the calculated ranges
129+
void init() {
130+
for (uint32_t j = 0; j < bins.size(); ++j) {
131+
Bin& bin(bins[j]);
132+
bin.clear();
133+
bin.range = calcrange(j);
134+
}
135+
totalcount = 0;
136+
totalsum = 0;
137+
}
138+
139139
//! Store the bin's statistics
140140
struct Bin {
141141
Range range;

tests/microstats/testMicroStats.cpp

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,78 +11,92 @@ uint64_t now() {
1111
#include <chrono>
1212
uint64_t now() {
1313
using std::chrono;
14-
return duration_cast<nanoseconds>(
15-
system_clock::now().time_since_epoch()).count();
14+
return duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
1615
}
1716
#endif
1817

19-
template< uint32_t N >
20-
void summary( const MicroStats<N>& ms )
21-
{
22-
constexpr double sd2 = 0.022750131948178987;
23-
constexpr double sd1 = 0.15865525393145702;
24-
std::cout << "10pct:" << ms.percentile(10)
25-
<< " -2SD:" << ms.percentile(100*sd2)
26-
<< " -1SD:" << ms.percentile(100*sd1)
27-
<< " 50pct:" << ms.percentile(50)
28-
<< " +1SD:" << ms.percentile(100-100*sd1)
29-
<< " +2SD:" << ms.percentile(100-200*sd2)
30-
<< " 90pct:" << ms.percentile(90)
31-
<< " 99pct:" << ms.percentile(99)
32-
<< " Stdev:" << (ms.percentile(100-100*sd1) - ms.percentile(100*sd1))/2
33-
<< " Stdev:" << (ms.percentile(100-100*sd2) - ms.percentile(100*sd2))/4
18+
template <uint32_t N>
19+
void summary(const std::string& title, const MicroStats<N>& ms) {
20+
constexpr double sd2 = 47.71;
21+
constexpr double sd1 = 34.13;
22+
std::cout << title << "\n\tPercentiles (1/10/50/90/99): " << ms.percentile(1) << " "
23+
<< ms.percentile(10) << " " << ms.percentile(50) << " " << ms.percentile(90)
24+
<< " " << ms.percentile(99) << " "
25+
<< "\n\tSD(-2/-1/+1/+2):" << ms.percentile(50 - sd2) << " "
26+
<< ms.percentile(50 - sd1) << " " << ms.percentile(50 + sd1) << " "
27+
<< ms.percentile(50 + sd2)
28+
<< "\n\tSd:" << (ms.percentile(50 + sd1) - ms.percentile(50 - sd1)) / 2
29+
<< "\n\t2Sd:" << (ms.percentile(50 + sd2) - ms.percentile(50 - sd2)) / 2
3430
<< '\n';
3531
}
3632

37-
int main( int argc, char* argv[] )
38-
{
39-
MicroStats<3> ms;
33+
int main(int argc, char* argv[]) {
34+
using Histogram = MicroStats<6>;
35+
int bin = Histogram::calcbin(1000);
36+
auto range = Histogram::calcrange(bin);
37+
std::cout << "Total number of bins: " << Histogram::NUMBINS << "\n";
38+
std::cout << "Bin for value 1000: " << bin << "\n";
39+
std::cout << "Range for bin " << bin << ": " << range.from << "-" << range.to << "\n";
4040

4141
// Single point distribution
42-
for ( uint32_t j=0; j<1000; ++j ) {
43-
ms.add( 1000 );
42+
Histogram ms;
43+
for (uint32_t j = 0; j < 1000; ++j) {
44+
ms.add(1000);
4445
}
45-
summary( ms );
46+
summary("Single Point Distribution at 1,000", ms);
4647
ms.clear();
4748

4849
// Uniform distribution over [0,10000]
49-
for ( uint32_t j=0; j<10000; ++j ) {
50-
ms.add( j );
50+
for (uint32_t j = 0; j < 10000; ++j) {
51+
ms.add(j);
5152
}
52-
summary( ms );
53+
summary("Uniform Distribution Over [0,10,000]", ms);
5354
ms.clear();
5455

5556
// Random distribution
56-
MicroStats<4> tms;
57+
Histogram tms;
58+
59+
bin = Histogram::calcbin(50000);
60+
range = Histogram::calcrange(bin);
61+
std::cout << "Bin for value 50000: " << bin;
62+
std::cout << " range: " << range.from << "-" << range.to << "\n";
63+
bin = Histogram::calcbin(50000 - 2500);
64+
range = Histogram::calcrange(bin);
65+
std::cout << "Bin for value 47500: " << bin;
66+
std::cout << " range: " << range.from << "-" << range.to << "\n";
67+
bin = Histogram::calcbin(50000 + 2500);
68+
range = Histogram::calcrange(bin);
69+
std::cout << "Bin for value 52500: " << bin;
70+
std::cout << " range: " << range.from << "-" << range.to << "\n";
71+
5772
std::default_random_engine generator;
58-
std::normal_distribution<double> distribution(50000.0,2500.0);
59-
for ( uint32_t j=0; j<10000000; ++j ) {
73+
std::normal_distribution<double> distribution(50000.0, 2500.0);
74+
for (uint32_t j = 0; j < 10000000; ++j) {
6075
double number = distribution(generator);
61-
if ( number<0 ) number=0;
62-
uint64_t unumber = ::llrint( number );
76+
if (number < 0) number = 0;
77+
uint64_t unumber = ::llrint(number);
6378
uint64_t t0 = now();
64-
if ( t0>0 ) {
65-
ms.add( unumber );
79+
if (t0 > 0) {
80+
ms.add(unumber);
6681
uint64_t t1 = now();
67-
if ( t1>t0 ) tms.add( t1-t0 );
82+
if (t1 > t0) tms.add(t1 - t0);
6883
}
6984
}
70-
summary( ms );
85+
summary("Normal Distribution (u=50,000, s=2,500)", ms);
7186
ms.clear();
7287

7388
std::cout << "Cost of MicroStats (measure+bin)" << '\n';
74-
summary( tms );
89+
summary("Microstats Cost", tms);
7590
tms.clear();
7691

77-
78-
for ( uint32_t j=0; j<10000000; ++j ) {
92+
for (uint32_t j = 0; j < 10000000; ++j) {
7993
uint64_t t0 = now();
80-
if ( t0>0 ) {
81-
//asm __volatile__( "nop" );
94+
if (t0 > 0) {
95+
// asm __volatile__( "nop" );
8296
uint64_t t1 = now();
83-
if ( t1>t0 ) tms.add( t1-t0 );
97+
if (t1 > t0) tms.add(t1 - t0);
8498
}
8599
}
86100
std::cout << "Cost of measuring only" << '\n';
87-
summary( tms );
101+
summary("Measuring Cost Only", tms);
88102
}

0 commit comments

Comments
 (0)