1- #if defined(__linux__) || (__APPLE__ && __aarch64__)
1+ #if defined(__linux__) || (__APPLE__ && __aarch64__)
22#define USING_COUNTERS
33#include " event_counter.h"
44#endif
2222#include < vector>
2323#include < locale.h>
2424
25-
2625template <typename CharT>
2726double findmax_fastfloat64 (std::vector<std::basic_string<CharT>> &s) {
2827 double answer = 0 ;
@@ -55,8 +54,9 @@ event_collector collector{};
5554
5655#ifdef USING_COUNTERS
5756template <class T , class CharT >
58- std::vector<event_count> time_it_ns (std::vector<std::basic_string<CharT>> &lines,
59- T const &function, size_t repeat) {
57+ std::vector<event_count>
58+ time_it_ns (std::vector<std::basic_string<CharT>> &lines, T const &function,
59+ size_t repeat) {
6060 std::vector<event_count> aggregate;
6161 bool printed_bug = false ;
6262 for (size_t i = 0 ; i < repeat; i++) {
@@ -71,7 +71,8 @@ std::vector<event_count> time_it_ns(std::vector<std::basic_string<CharT>> &lines
7171 return aggregate;
7272}
7373
74- void pretty_print (double volume, size_t number_of_floats, std::string name, std::vector<event_count> events) {
74+ void pretty_print (double volume, size_t number_of_floats, std::string name,
75+ std::vector<event_count> events) {
7576 double volumeMB = volume / (1024 . * 1024 .);
7677 double average_ns{0 };
7778 double min_ns{DBL_MAX};
@@ -83,7 +84,7 @@ void pretty_print(double volume, size_t number_of_floats, std::string name, std:
8384 double branches_avg{0 };
8485 double branch_misses_min{0 };
8586 double branch_misses_avg{0 };
86- for (event_count e : events) {
87+ for (event_count e : events) {
8788 double ns = e.elapsed_ns ();
8889 average_ns += ns;
8990 min_ns = min_ns < ns ? min_ns : ns;
@@ -94,51 +95,46 @@ void pretty_print(double volume, size_t number_of_floats, std::string name, std:
9495
9596 double instructions = e.instructions ();
9697 instructions_avg += instructions;
97- instructions_min = instructions_min < instructions ? instructions_min : instructions;
98+ instructions_min =
99+ instructions_min < instructions ? instructions_min : instructions;
98100
99101 double branches = e.branches ();
100102 branches_avg += branches;
101103 branches_min = branches_min < branches ? branches_min : branches;
102104
103105 double branch_misses = e.missed_branches ();
104106 branch_misses_avg += branch_misses;
105- branch_misses_min = branch_misses_min < branch_misses ? branch_misses_min : branch_misses;
107+ branch_misses_min =
108+ branch_misses_min < branch_misses ? branch_misses_min : branch_misses;
106109 }
107110 cycles_avg /= events.size ();
108111 instructions_avg /= events.size ();
109112 average_ns /= events.size ();
110113 branches_avg /= events.size ();
111114 printf (" %-40s: %8.2f MB/s (+/- %.1f %%) " , name.data (),
112- volumeMB * 1000000000 / min_ns,
113- (average_ns - min_ns) * 100.0 / average_ns);
114- printf (" %8.2f Mfloat/s " ,
115- number_of_floats * 1000 / min_ns);
116- if (instructions_min > 0 ) {
117- printf (" %8.2f i/B %8.2f i/f (+/- %.1f %%) " ,
118- instructions_min / volume,
119- instructions_min / number_of_floats,
115+ volumeMB * 1000000000 / min_ns,
116+ (average_ns - min_ns) * 100.0 / average_ns);
117+ printf (" %8.2f Mfloat/s " , number_of_floats * 1000 / min_ns);
118+ if (instructions_min > 0 ) {
119+ printf (" %8.2f i/B %8.2f i/f (+/- %.1f %%) " , instructions_min / volume,
120+ instructions_min / number_of_floats,
120121 (instructions_avg - instructions_min) * 100.0 / instructions_avg);
121122
122- printf (" %8.2f c/B %8.2f c/f (+/- %.1f %%) " ,
123- cycles_min / volume,
124- cycles_min / number_of_floats,
123+ printf (" %8.2f c/B %8.2f c/f (+/- %.1f %%) " , cycles_min / volume,
124+ cycles_min / number_of_floats,
125125 (cycles_avg - cycles_min) * 100.0 / cycles_avg);
126- printf (" %8.2f i/c " ,
127- instructions_min /cycles_min);
128- printf (" %8.2f b/f " ,
129- branches_avg /number_of_floats);
130- printf (" %8.2f bm/f " ,
131- branch_misses_avg /number_of_floats);
132- printf (" %8.2f GHz " ,
133- cycles_min / min_ns);
126+ printf (" %8.2f i/c " , instructions_min / cycles_min);
127+ printf (" %8.2f b/f " , branches_avg / number_of_floats);
128+ printf (" %8.2f bm/f " , branch_misses_avg / number_of_floats);
129+ printf (" %8.2f GHz " , cycles_min / min_ns);
134130 }
135131 printf (" \n " );
136-
137132}
138133#else
139134template <class T , class CharT >
140- std::pair<double , double > time_it_ns (std::vector<std::basic_string<CharT>> &lines,
141- T const &function, size_t repeat) {
135+ std::pair<double , double >
136+ time_it_ns (std::vector<std::basic_string<CharT>> &lines, T const &function,
137+ size_t repeat) {
142138 std::chrono::high_resolution_clock::time_point t1, t2;
143139 double average = 0 ;
144140 double min_value = DBL_MAX;
@@ -160,21 +156,16 @@ std::pair<double, double> time_it_ns(std::vector<std::basic_string<CharT>> &line
160156 return std::make_pair (min_value, average);
161157}
162158
163-
164-
165-
166- void pretty_print (double volume, size_t number_of_floats, std::string name, std::pair<double ,double > result) {
159+ void pretty_print (double volume, size_t number_of_floats, std::string name,
160+ std::pair<double , double > result) {
167161 double volumeMB = volume / (1024 . * 1024 .);
168162 printf (" %-40s: %8.2f MB/s (+/- %.1f %%) " , name.data (),
169- volumeMB * 1000000000 / result.first ,
170- (result.second - result.first ) * 100.0 / result.second );
171- printf (" %8.2f Mfloat/s " ,
172- number_of_floats * 1000 / result.first );
173- printf (" %8.2f ns/f \n " ,
174- double (result.first ) /number_of_floats );
163+ volumeMB * 1000000000 / result.first ,
164+ (result.second - result.first ) * 100.0 / result.second );
165+ printf (" %8.2f Mfloat/s " , number_of_floats * 1000 / result.first );
166+ printf (" %8.2f ns/f \n " , double (result.first ) / number_of_floats);
175167}
176- #endif
177-
168+ #endif
178169
179170// this is okay, all chars are ASCII
180171inline std::u16string widen (std::string line) {
@@ -195,21 +186,23 @@ std::vector<std::u16string> widen(const std::vector<std::string> &lines) {
195186 return u16lines;
196187}
197188
198-
199189void process (std::vector<std::string> &lines, size_t volume) {
200190 size_t repeat = 100 ;
201191 double volumeMB = volume / (1024 . * 1024 .);
202192 std::cout << " ASCII volume = " << volumeMB << " MB " << std::endl;
203- pretty_print (volume, lines.size (), " fastfloat (64)" , time_it_ns (lines, findmax_fastfloat64<char >, repeat));
204- pretty_print (volume, lines.size (), " fastfloat (32)" , time_it_ns (lines, findmax_fastfloat32<char >, repeat));
193+ pretty_print (volume, lines.size (), " fastfloat (64)" ,
194+ time_it_ns (lines, findmax_fastfloat64<char >, repeat));
195+ pretty_print (volume, lines.size (), " fastfloat (32)" ,
196+ time_it_ns (lines, findmax_fastfloat32<char >, repeat));
205197
206198 std::vector<std::u16string> lines16 = widen (lines);
207199 volume = 2 * volume;
208200 volumeMB = volume / (1024 . * 1024 .);
209201 std::cout << " UTF-16 volume = " << volumeMB << " MB " << std::endl;
210- pretty_print (volume, lines.size (), " fastfloat (64)" , time_it_ns (lines16, findmax_fastfloat64<char16_t >, repeat));
211- pretty_print (volume, lines.size (), " fastfloat (32)" , time_it_ns (lines16, findmax_fastfloat32<char16_t >, repeat));
212-
202+ pretty_print (volume, lines.size (), " fastfloat (64)" ,
203+ time_it_ns (lines16, findmax_fastfloat64<char16_t >, repeat));
204+ pretty_print (volume, lines.size (), " fastfloat (32)" ,
205+ time_it_ns (lines16, findmax_fastfloat32<char16_t >, repeat));
213206}
214207
215208void fileload (std::string filename) {
@@ -233,13 +226,14 @@ void fileload(std::string filename) {
233226 process (lines, volume);
234227}
235228
236-
237229int main (int argc, char **argv) {
238- if (collector.has_events ()) {
230+ if (collector.has_events ()) {
239231 std::cout << " # Using hardware counters" << std::endl;
240232 } else {
241- #if defined(__linux__) || (__APPLE__ && __aarch64__)
242- std::cout << " # Hardware counters not available, try to run in privileged mode (e.g., sudo)." << std::endl;
233+ #if defined(__linux__) || (__APPLE__ && __aarch64__)
234+ std::cout << " # Hardware counters not available, try to run in privileged "
235+ " mode (e.g., sudo)."
236+ << std::endl;
243237#endif
244238 }
245239 fileload (std::string (BENCHMARK_DATA_DIR) + " /canada.txt" );
0 commit comments