changeset 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
files Histogram.c Histogram.h
diffstat 2 files changed, 42 insertions(+), 7 deletions(-) [+]
line diff
     1.1 --- a/Histogram.c	Sun Nov 14 11:11:44 2010 -0800
     1.2 +++ b/Histogram.c	Sat Nov 20 08:41:39 2010 +0100
     1.3 @@ -109,6 +109,25 @@
     1.4     hist->bins[ binIdx ] += 1;
     1.5   }
     1.6  
     1.7 +void inline
     1.8 +subFromHist( int32 value, Histogram *hist )
     1.9 + {
    1.10 +   int32 binIdx;
    1.11 +
    1.12 +   if( value < hist->startOfRange )
    1.13 +    { binIdx = 0;
    1.14 +    }
    1.15 +   else if( value > hist->endOfRange )
    1.16 +    { binIdx = hist->numBins - 1;
    1.17 +    }
    1.18 +   else
    1.19 +    {
    1.20 +      binIdx = (value - hist->startOfRange) / hist->binWidth;
    1.21 +    }
    1.22 +
    1.23 +   hist->bins[ binIdx ] -= 1;
    1.24 + }
    1.25 +
    1.26  
    1.27  /*Inline because use with RDTSC in innermost code so need ultra-fast
    1.28   */
    1.29 @@ -122,13 +141,24 @@
    1.30     addToHist( value, hist );
    1.31   }
    1.32  
    1.33 +void inline
    1.34 +subIntervalFromHist( int32 startIntvl, int32 endIntvl, Histogram *hist )
    1.35 + {
    1.36 +   int32 value;
    1.37 +
    1.38 +   value = endIntvl - startIntvl;
    1.39 +   if( value < 0 || value > 10000000 ) return; //sanity check
    1.40 +   subFromHist( value, hist );
    1.41 + }
    1.42 +
    1.43  void
    1.44  printHist( Histogram *hist )
    1.45   {
    1.46     int32   binIdx, i, numBars, maxHeight, barValue, binStart, binEnd;
    1.47 -   float32 total, binPercent, expectedValue1, expectedValue2;
    1.48 +   float32 total, total2, binPercent, expectedValue1, expectedValue2;
    1.49  
    1.50 -
    1.51 +   if( hist == NULL ) return;
    1.52 +   
    1.53        //do all except the top bin
    1.54     maxHeight = 0; total = 0.0; expectedValue1 = 0.0;
    1.55     for( i = 0; i < hist->numBins -1; i++ )
    1.56 @@ -139,9 +169,9 @@
    1.57        expectedValue1 += hist->bins[ i ] * (binStart + hist->binWidth/2.0);
    1.58      }
    1.59        //copy and calc expected value minus the top bin
    1.60 -   expectedValue2 = expectedValue1;
    1.61 +   expectedValue2  = expectedValue1;
    1.62     expectedValue2 /= total;
    1.63 -
    1.64 +   total2          = total;
    1.65        //now do last iteration, to add the top bin
    1.66     if( maxHeight < hist->bins[ i ] ) maxHeight = hist->bins[ i ];
    1.67     total += hist->bins[ i ];
    1.68 @@ -155,10 +185,12 @@
    1.69     printf( "histogram: " );
    1.70     if( hist->name != NULL ) printf( "%s\n", hist->name );
    1.71     else printf( "\n" );
    1.72 -   printf( "expected value: %3.2f \n", expectedValue1 );
    1.73 -   printf( "expected value minus top bin: %3.2f \n", expectedValue2 );
    1.74 +   printf( "               num samples: %d | expected value: %3.2f \n",
    1.75 +                                               (int)total, expectedValue1 );
    1.76 +   printf( "minus top bin, num samples: %d | expected value: %3.2f \n",
    1.77 +                                               (int)total2, expectedValue2 );
    1.78  
    1.79 -   if( barValue == 0 ) printf("error printing histogram\n");
    1.80 +   if( barValue == 0 ) { printf("error: bar val zero\n"); return; }
    1.81     for( binIdx = 0; binIdx < hist->numBins; binIdx++ )
    1.82      {
    1.83        binStart = hist->startOfRange + hist->binWidth * binIdx;
     2.1 --- a/Histogram.h	Sun Nov 14 11:11:44 2010 -0800
     2.2 +++ b/Histogram.h	Sat Nov 20 08:41:39 2010 +0100
     2.3 @@ -59,6 +59,9 @@
     2.4  void inline
     2.5  addIntervalToHist( int32 startIntvl, int32 endIntvl, Histogram *hist );
     2.6  
     2.7 +void inline
     2.8 +subIntervalFromHist( int32 startIntvl, int32 endIntvl, Histogram *hist );
     2.9 +
    2.10  void
    2.11  printHist( Histogram *hist );
    2.12