-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cc
131 lines (112 loc) · 2.7 KB
/
util.cc
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
125
126
127
128
129
130
131
#include "includes.h"
#include "util.h"
uint64_t get_time_us ()
{
struct timeval tv;
gettimeofday (&tv, NULL);
return tv.tv_sec * 1000000ULL + tv.tv_usec;
}
float frand ()
{
return (rand () % 10000000) / 10000000.f;
}
int next_power_of_two (int value)
{
int ret = 1;
while (ret < value)
ret *= 2;
return ret;
}
void clear_line ()
{
printf ("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
}
static void print_char (float v)
{
if (v >= 8.f / 8.f)
printf ("█");
else if (v >= 7.f / 8.f)
printf ("▉");
else if (v >= 6.f / 8.f)
printf ("▊");
else if (v >= 5.f / 8.f)
printf ("▋");
else if (v >= 4.f / 8.f)
printf ("▌");
else if (v >= 3.f / 8.f)
printf ("▍");
else if (v >= 2.f / 8.f)
printf ("▎");
else if (v >= 1.f / 8.f)
printf ("▏");
else
printf (" ");
}
void print_bar (float value)
{
int printed_char = 0;
while ((value > 0.f) && (printed_char < 110)) {
printed_char++;
if (printed_char == 107) {
printf ("▶");
}
else if (printed_char == 108) {
printf (COLOR_INVERT);
printf ("▶");
printf (COLOR_UNINVERT);
}
else {
print_char (value);
value -= 1.f;
}
}
while (printed_char < 110) {
printed_char++;
printf (" ");
}
}
void print_histogram (std::vector < float >values)
{
const int num_buckets = 50;
float buckets[num_buckets];
float min_value = FLT_MAX;
float max_value = -FLT_MAX;
float expectancy = 0.f;
for (unsigned i = 0; i < values.size (); i++) {
if (values[i] < min_value)
min_value = values[i];
if (values[i] > max_value)
max_value = values[i];
expectancy += values[i];
}
expectancy /= (float) values.size ();
printf ("Value range %f - %f\n", min_value, max_value);
min_value = 0.f;
max_value = 3.f;
for (int b = 0; b < num_buckets; b++)
buckets[b] = 0.f;
int break_even_bucket = (int) ((float) num_buckets * (1.0f - min_value) / (max_value - min_value));
for (unsigned i = 0; i < values.size (); i++) {
int bucket_index = (int) (num_buckets * (values[i] - min_value) / (max_value - min_value));
// Fold the tail of the distribution.
if (bucket_index >= num_buckets)
bucket_index = num_buckets - 1;
buckets[bucket_index] += 0.00012f;
}
float total = 0.00012f * values.size ();
float counted = 0.f;
for (int b = 0; b < num_buckets; b++) {
float value = buckets[b];
counted += value;
if (b < break_even_bucket)
printf (COLOR_RED);
else if (b == break_even_bucket)
printf (COLOR_YELLOW);
else
printf (COLOR_GREEN);
print_bar (value);
printf ("│ %.0f%% e=%f", 100.f * counted / total, (float) b / (float) num_buckets * (max_value - min_value) + min_value);
printf ("\n");
}
printf (COLOR_NORMAL);
}