comparison Histogram.c @ 8:c83c27796fad

Added sub from hist, added "total" to print and return when bar is zero
author Me
date Sat, 20 Nov 2010 08:41:39 +0100
parents fa6a281bd854
children 060d63cb5d34
comparison
equal deleted inserted replaced
5:4092e6d96a57 6:b3eab9829935
107 } 107 }
108 108
109 hist->bins[ binIdx ] += 1; 109 hist->bins[ binIdx ] += 1;
110 } 110 }
111 111
112 void inline
113 subFromHist( int32 value, Histogram *hist )
114 {
115 int32 binIdx;
116
117 if( value < hist->startOfRange )
118 { binIdx = 0;
119 }
120 else if( value > hist->endOfRange )
121 { binIdx = hist->numBins - 1;
122 }
123 else
124 {
125 binIdx = (value - hist->startOfRange) / hist->binWidth;
126 }
127
128 hist->bins[ binIdx ] -= 1;
129 }
130
112 131
113 /*Inline because use with RDTSC in innermost code so need ultra-fast 132 /*Inline because use with RDTSC in innermost code so need ultra-fast
114 */ 133 */
115 void inline 134 void inline
116 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist ) 135 addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
120 value = endIntvl - startIntvl; 139 value = endIntvl - startIntvl;
121 if( value < 0 || value > 10000000 ) return; //sanity check 140 if( value < 0 || value > 10000000 ) return; //sanity check
122 addToHist( value, hist ); 141 addToHist( value, hist );
123 } 142 }
124 143
144 void inline
145 subIntervalFromHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
146 {
147 int32 value;
148
149 value = endIntvl - startIntvl;
150 if( value < 0 || value > 10000000 ) return; //sanity check
151 subFromHist( value, hist );
152 }
153
125 void 154 void
126 printHist( Histogram *hist ) 155 printHist( Histogram *hist )
127 { 156 {
128 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd; 157 int32 binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
129 float32 total, binPercent, expectedValue1, expectedValue2; 158 float32 total, total2, binPercent, expectedValue1, expectedValue2;
130 159
131 160 if( hist == NULL ) return;
161
132 //do all except the top bin 162 //do all except the top bin
133 maxHeight = 0; total = 0.0; expectedValue1 = 0.0; 163 maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
134 for( i = 0; i < hist->numBins -1; i++ ) 164 for( i = 0; i < hist->numBins -1; i++ )
135 { 165 {
136 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 166 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
137 total += hist->bins[ i ]; 167 total += hist->bins[ i ];
138 binStart = hist->startOfRange + hist->binWidth * i; 168 binStart = hist->startOfRange + hist->binWidth * i;
139 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); 169 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
140 } 170 }
141 //copy and calc expected value minus the top bin 171 //copy and calc expected value minus the top bin
142 expectedValue2 = expectedValue1; 172 expectedValue2 = expectedValue1;
143 expectedValue2 /= total; 173 expectedValue2 /= total;
144 174 total2 = total;
145 //now do last iteration, to add the top bin 175 //now do last iteration, to add the top bin
146 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ]; 176 if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
147 total += hist->bins[ i ]; 177 total += hist->bins[ i ];
148 binStart = hist->startOfRange + hist->binWidth * i; 178 binStart = hist->startOfRange + hist->binWidth * i;
149 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0); 179 expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
153 barValue = maxHeight / 60; //60 spaces across page for tallest bin 183 barValue = maxHeight / 60; //60 spaces across page for tallest bin
154 184
155 printf( "histogram: " ); 185 printf( "histogram: " );
156 if( hist->name != NULL ) printf( "%s\n", hist->name ); 186 if( hist->name != NULL ) printf( "%s\n", hist->name );
157 else printf( "\n" ); 187 else printf( "\n" );
158 printf( "expected value: %3.2f \n", expectedValue1 ); 188 printf( " num samples: %d | expected value: %3.2f \n",
159 printf( "expected value minus top bin: %3.2f \n", expectedValue2 ); 189 (int)total, expectedValue1 );
160 190 printf( "minus top bin, num samples: %d | expected value: %3.2f \n",
161 if( barValue == 0 ) printf("error printing histogram\n"); 191 (int)total2, expectedValue2 );
192
193 if( barValue == 0 ) { printf("error: bar val zero\n"); return; }
162 for( binIdx = 0; binIdx < hist->numBins; binIdx++ ) 194 for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
163 { 195 {
164 binStart = hist->startOfRange + hist->binWidth * binIdx; 196 binStart = hist->startOfRange + hist->binWidth * binIdx;
165 binEnd = binStart + hist->binWidth - 1; 197 binEnd = binStart + hist->binWidth - 1;
166 binPercent = 100 * hist->bins[ binIdx ] / total; 198 binPercent = 100 * hist->bins[ binIdx ] / total;