@@ -11,78 +11,92 @@ uint64_t now() {
1111#include < chrono>
1212uint64_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\t Percentiles (1/10/50/90/99): " << ms.percentile (1 ) << " "
23+ << ms.percentile (10 ) << " " << ms.percentile (50 ) << " " << ms.percentile (90 )
24+ << " " << ms.percentile (99 ) << " "
25+ << " \n\t SD(-2/-1/+1/+2):" << ms.percentile (50 - sd2) << " "
26+ << ms.percentile (50 - sd1) << " " << ms.percentile (50 + sd1) << " "
27+ << ms.percentile (50 + sd2)
28+ << " \n\t Sd:" << (ms.percentile (50 + sd1) - ms.percentile (50 - sd1)) / 2
29+ << " \n\t 2Sd:" << (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