-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathhist_utils.c
124 lines (115 loc) · 1.94 KB
/
hist_utils.c
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "hist_utils.h"
#include <stdio.h>
#include <math.h>
void
hist_add(struct histogram *hist, int len, size_t val)
{
// remove outliers
int j = val;
if (j < 800) // remove outliers
{
while (hist[j % len].val > 0 && hist[j % len].val != (int)val) {
j++;
}
hist[j % len].val = val;
hist[j % len].count++;
}
}
float
hist_avg(struct histogram *hist, int len) {
float total = 0;
int i = 0, n = 0;
for (i=0; i < len; i++) {
if (hist[i].val > 0) {
total += hist[i].val * hist[i].count;
n += hist[i].count;
}
}
return (float)(total / n);
}
int
hist_mode(struct histogram *hist, int len)
{
int i, max = 0, mode = 0;
for (i=0; i < len; i++)
{
if (hist[i].count > max)
{
max = hist[i].count;
mode = hist[i].val;
}
}
return mode;
}
int
hist_min(struct histogram *hist, int len)
{
int i, min = 99999;
for (i=0; i < len; i++)
{
if (hist[i].count > 0 && hist[i].val < min)
{
min = hist[i].val;
}
}
return min;
}
int
hist_max(struct histogram *hist, int len)
{
int i, max= 0;
for (i=0; i < len; i++)
{
if (hist[i].count > 0 && hist[i].val > max)
{
max = hist[i].val;
}
}
return max;
}
double
hist_variance(struct histogram *hist, int len, int mean)
{
int i, count = 0;
double sum = 0;
for (i=0; i < len; i++)
{
if (hist[i].count > 0) {
sum += pow((double)(hist[i].val - mean), 2.0) * hist[i].count;
count += hist[i].count;
}
}
return sum / count;
}
double
hist_std(struct histogram *hist, int len, int mean)
{
return sqrt(hist_variance (hist, len, mean));
}
// count number of misses
int
hist_q(struct histogram *hist, int len, int threshold)
{
int i = 0, count = 0;
for (i=0; i < len; i++)
{
if (hist[i].count > 0 && hist[i].val > threshold)
{
count += hist[i].count;
}
}
return count;
}
void
hist_print(struct histogram *hist, int len)
{
int i = 0;
for (i=0; i < len; i++)
{
if (hist[i].count > 0)
{
printf("%d(%d) ", hist[i].val, hist[i].count);
}
}
printf("\n");
}