-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark_old.cpp
141 lines (126 loc) · 3.92 KB
/
benchmark_old.cpp
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
132
133
134
135
136
137
138
139
140
141
#include "fast_float/fast_float.h"
#include "performancecounters/event_counter.h"
#include <algorithm>
#include <charconv>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <locale.h>
#include <random>
#include <sstream>
#include <stdio.h>
#include <vector>
#include "random_generators.h"
double findmax_strtod(std::vector<std::string> &s) {
double answer = 0;
double x = 0;
for (std::string &st : s) {
char *pr = (char *)st.data();
static locale_t c_locale = newlocale(LC_ALL_MASK, "C", NULL);
x = strtod_l(st.data(), &pr, c_locale);
if (pr == st.data()) {
throw std::runtime_error("bug in findmax_strtod");
}
answer = answer > x ? answer : x;
}
return answer;
}
double findmax_fastfloat(std::vector<std::string> &s) {
double answer = 0;
double x = 0;
for (std::string &st : s) {
auto [p, ec] = fast_float::from_chars(st.data(), st.data() + st.size(), x);
if (p == st.data()) {
throw std::runtime_error("bug in findmax_fastfloat");
}
answer = answer > x ? answer : x;
}
return answer;
}
event_collector collector;
template <class T>
event_aggregate time_it_ns(std::vector<std::string> &lines, T const &function, size_t repeat) {
event_aggregate aggregate{};
// warm up the cache:
for (size_t i = 0; i < 10; i++) {
double ts = function(lines);
if (ts == 0) {
printf("bug\n");
}
}
for (size_t i = 0; i < repeat; i++) {
collector.start();
double ts = function(lines);
if (ts == 0) {
printf("bug\n");
}
event_count allocate_count = collector.end();
aggregate << allocate_count;
}
return aggregate;
}
void pretty_print(
double volume, size_t number_of_floats, std::string name,
event_aggregate aggregate) {
(void)volume;
(void)number_of_floats;
printf(" %32s ", name.c_str());
printf(" %8.2f ns/float ", aggregate.elapsed_ns() / number_of_floats);
printf(" %8.2f instructions/float ", aggregate.best.instructions() / number_of_floats);
printf("\n");
printf(" %32s ", "");
printf(" %8.2f cycles/float ", aggregate.best.cycles() / number_of_floats);
printf("\n");
printf(" %32s ", "");
printf(" %8.2f branches/float ", aggregate.best.branches() / number_of_floats);
printf("\n");
printf(" %32s ", "");
printf(" %8.2f branch miss/float ", aggregate.best.branch_misses() / number_of_floats);
printf(" %32s ", "");
printf(" %8.2f instructions/cycle ",
aggregate.best.instructions() / aggregate.best.cycles());
}
void process(std::vector<std::string> &lines, size_t volume) {
size_t repeat = 100;
double volumeMB = volume / (1024. * 1024.);
std::cout << "volume = " << volumeMB << " MB " << std::endl;
pretty_print(volume, lines.size(), "strtod",
time_it_ns(lines, findmax_strtod, repeat));
printf("\n");
pretty_print(volume, lines.size(), "fastfloat",
time_it_ns(lines, findmax_fastfloat, repeat));
printf("\n");
}
void parse_random_numbers() {
std::cout << "# parsing random numbers" << std::endl;
std::vector<std::string> lines;
size_t howmany{10000};
auto g = std::unique_ptr<string_number_generator>(
get_generator_by_name("uniform"));
std::cout << "model: " << g->describe() << std::endl;
std::cout << "volume: " << howmany << " floats" << std::endl;
lines.reserve(howmany); // let us reserve plenty of memory.
size_t volume = 0;
for (size_t i = 0; i < howmany; i++) {
std::string line = g->new_string();
volume += line.size();
lines.push_back(line);
}
process(lines, volume);
}
int main() {
parse_random_numbers();
if(!collector.has_events()) {
std::cerr << "I cannot access the performance counters. Make sure you run the program in privileged mode (e.g., sudo) under Linux our macOS/ARM." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}