]> rtime.felk.cvut.cz Git - can-benchmark.git/blob - latester/histogram.h
64dcb2220804d0ab75c11783735f49b33ee9723f
[can-benchmark.git] / latester / histogram.h
1 #ifndef HISTOGRAM_H
2 #define HISTOGRAM_H
3
4 struct histogram {
5         unsigned *data;
6         unsigned allocated;
7         unsigned resolution;
8 };
9
10 int histogram_init(struct histogram *h,
11                    unsigned max_value,
12                    unsigned resolution)
13 {
14         size_t mem;
15         h->allocated = max_value/resolution + 1;
16         h->resolution = resolution;
17         mem = h->allocated*sizeof(*h->data);
18         h->data = malloc(mem);
19         if (h->data) {
20                 memset(h->data, 0, mem);
21                 return 0;
22         } else
23                 return -1;
24 }
25
26 void histogram_add(struct histogram *h, int value)
27 {
28         if (value < 0) value = 0;
29         unsigned index = value / h->resolution;
30         if (index >= h->allocated)
31                 index = h->allocated - 1;
32         h->data[index]++;
33 }
34
35 void histogram_fprint(struct histogram *h, FILE *f)
36 {
37         unsigned long long sum = 0, cum;
38         unsigned i;
39         for (i = 0; i < h->allocated; i++)
40                 sum += h->data[i];
41         cum = sum;
42         for (i = 0; i < h->allocated; i++) {
43                 if (h->data[i] != 0) {
44                         fprintf(f, "%g %lld\n", 1e-3*(i*h->resolution), cum);
45                 }
46                 cum -= h->data[i];
47         }
48 }
49
50
51
52 #endif