-
Notifications
You must be signed in to change notification settings - Fork 116
/
kvstats.hh
53 lines (51 loc) · 1.62 KB
/
kvstats.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef KVSTATS_HH
#define KVSTATS_HH 1
#include <stdlib.h>
struct kvstats {
double min, max, sum, sumsq;
long count;
kvstats()
: min(-1), max(-1), sum(0), sumsq(0), count(0) {
}
void add(double x) {
if (!count || x < min)
min = x;
if (max < x)
max = x;
sum += x;
sumsq += x * x;
count += 1;
}
typedef void (kvstats::*unspecified_bool_type)(double);
operator unspecified_bool_type() const {
return count ? &kvstats::add : 0;
}
void print_report(const char *name) const {
if (count)
printf("%s: n %ld, total %.0f, average %.0f, min %.0f, max %.0f, stddev %.0f\n",
name, count, sum, sum / count, min, max,
sqrt((sumsq - sum * sum / count) / (count - 1)));
}
double avg() {
if (count)
return sum / count;
else
return 0;
}
};
#endif