int main(int argc, char **argv) { // use bucket size of .5 struct histogram *h = histogram_create(.5); // bucket [3.0, 3.5) histogram_insert(h, 3.00); histogram_insert(h, 3.14); // bucket [21.5, 22.0) histogram_insert(h, 21.99); // bucket [22.0, 22.5) histogram_insert(h, 22.00); histogram_insert(h, 22.20); histogram_insert(h, 22.49); // bucket [22.5, 23.0) histogram_insert(h, 22.50); histogram_insert(h, 22.99); // bucket [-22.0, -21.5) histogram_insert(h, -21.51); histogram_insert(h, -22.00); // bucket [-21.5, -21.0) histogram_insert(h, -21.49); histogram_insert(h, -21.20); histogram_insert(h, -21.01); double *buckets = histogram_buckets(h); double b = histogram_bucket_size(h); int expected_counts[] = {2, 3, 2, 1, 3, 2}; int i; for(i = 0; i < histogram_size(h); i++) { double start = buckets[i]; int count = histogram_count(h, start); if(expected_counts[i] != count) { fprintf(stderr, "Expected a count of %d, got %d.", expected_counts[i], count); return -1; } fprintf(stdout, "[%6.2lf, %6.2f) count: %d\n", start, start + b, histogram_count(h, start)); } fprintf(stdout, "max: %6.2lf\n", histogram_max_value(h)); fprintf(stdout, "min: %6.2lf\n", histogram_min_value(h)); fprintf(stdout, "mode: %6.2lf\n", histogram_mode(h)); fprintf(stdout, "mode count: %d\n", histogram_count(h, histogram_mode(h))); free(buckets); histogram_delete(h); return 0; }
int histogram_insert(struct histogram *h, double value) { uint64_t bucket = bucket_of(h, value); struct box_count *box = itable_lookup(h->buckets, bucket); if(!box) { box = calloc(1, sizeof(*box)); itable_insert(h->buckets, bucket, box); } h->total_count++; box->count++; int mode_count = histogram_count(h, histogram_mode(h)); if(value > h->max_value || h->total_count < 1) { h->max_value = value; } if(value < h->min_value || h->total_count < 1) { h->min_value = value; } if(box->count > mode_count) { h->mode = end_of(h, bucket); } return box->count; }
void category_first_allocation_accum_times(struct histogram *h, double *keys, double *tau_mean, double *counts_cdp, double *times_accum) { int n = histogram_size(h); double *times_mean = malloc(n*sizeof(double)); double *counts = malloc(n*sizeof(double)); // accumulate counts and compute mean times per bucket... int i; for(i = 0; i < n; i++) { int count = histogram_count(h, keys[i]); double *time_value = (double *) histogram_get_data(h, keys[i]); counts[i] = count; times_mean[i] = (*time_value)/count; } //... and compute the probability distribution and cumulative for(i = 0; i < n; i++) { counts_cdp[i] = (i > 0 ? counts_cdp[i-1] : 0) + counts[i]; } for(i = 0; i < n; i++) { counts[i] /= counts_cdp[n-1]; counts_cdp[i] /= counts_cdp[n-1]; } // compute proportion of mean time for buckets larger than i, for each i. for(i = n-1; i >= 0; i--) { // base case if(i == n-1) { times_accum[i] = 0; } else { times_accum[i] = times_accum[i+1] + (times_mean[i+1] * counts[i+1]); } } // set overall mean time *tau_mean = times_accum[0] + (times_mean[0] * counts[0]); free(counts); free(times_mean); }