2323#include < string>
2424#include < variant>
2525#include < fast_float/fast_float.h>
26+ #include < fmt/core.h>
2627
2728using Benchmarks::arithmetic_float;
2829using Benchmarks::BenchArgs;
@@ -47,12 +48,12 @@ void evaluateProperties(const std::vector<T> &lines,
4748
4849 for (const auto &algo : args) {
4950 if (!algo.used ) {
50- std::cout << " # skipping " << algo.name << std::endl ;
51+ fmt::println ( " # skipping {} " , algo.name ) ;
5152 continue ;
5253 }
5354 // Apply filter if provided
5455 if (!is_matched (algo.name , filter)) {
55- std::cout << " # filtered out " << algo.name << std::endl ;
56+ fmt::println ( " # filtered out {} " , algo.name ) ;
5657 continue ;
5758 }
5859 char buf1[100 ], buf2[100 ];
@@ -93,7 +94,7 @@ struct diy_float_t {
9394template <arithmetic_float T>
9495void process (const std::vector<T> &lines,
9596 const std::array<BenchArgs<T>, Benchmarks::COUNT> &args, const std::span<std::string> filter = {}) {
96- // We have a special algorithm for the reference :
97+ // We have a special algorithm for the string generation :
9798 std::string just_string = " just_string" ;
9899 if (is_matched (just_string, filter)) {
99100 std::vector<diy_float_t > parsed;
@@ -110,17 +111,16 @@ void process(const std::vector<T> &lines,
110111 return volume;
111112 }, 100 );
112113 } else {
113- std::cout << " # skipping " << just_string << std::endl;
114-
114+ fmt::println (" # skipping {}" , just_string);
115115 }
116116 for (const auto &algo : args) {
117117 if (!algo.used ) {
118- std::cout << " # skipping " << algo.name << std::endl ;
118+ fmt::println ( " # skipping {} " , algo.name ) ;
119119 continue ;
120120 }
121121 // Apply filter if provided
122122 if (!is_matched (algo.name , filter)) {
123- std::cout << " # filtered out " << algo.name << std::endl ;
123+ fmt::println ( " # filtered out {} " , algo.name ) ;
124124 continue ;
125125 }
126126 pretty_print (lines, algo.name , [&algo](const std::vector<T> &lines) -> int {
@@ -139,7 +139,7 @@ template <typename T>
139139std::vector<T> fileload (const std::string &filename) {
140140 std::ifstream inputfile (filename);
141141 if (!inputfile) {
142- std::cerr << " can't open " << filename << std::endl ;
142+ fmt::print (stderr, " can't open {} \n " , filename) ;
143143 return {};
144144 }
145145
@@ -150,24 +150,21 @@ std::vector<T> fileload(const std::string &filename) {
150150 lines.push_back (std::is_same_v<T, float > ? std::stof (line)
151151 : std::stod (line));
152152 } catch (...) {
153- std::cerr << " problem with " << line << " \n "
154- << " We expect floating-point numbers (one per line)."
155- << std::endl;
153+ fmt::print (stderr, " problem with {}\n We expect floating-point numbers (one per line).\n " , line);
156154 std::abort ();
157155 }
158156 }
159- std::cout << " # read " << lines.size () << " lines " << std::endl ;
157+ fmt::println ( " # read {} lines " , lines.size ()) ;
160158 return lines;
161159}
162160
163161template <typename T>
164162std::vector<T> get_random_numbers (size_t howmany,
165163 const std::string &random_model) {
166- std::cout << " # parsing random numbers" << std::endl ;
164+ fmt::println ( " # parsing random numbers" ) ;
167165 std::vector<T> lines;
168166 auto g = get_generator_by_name<T>(random_model);
169- std::cout << " model: " << g->describe () << " \n "
170- << " volume: " << howmany << " floats" << std::endl;
167+ fmt::print (" model: {}\n volume: {} floats\n " , g->describe (), howmany);
171168 lines.reserve (howmany); // let us reserve plenty of memory.
172169 for (size_t i = 0 ; i < howmany; i++) {
173170 const T line = g->new_float ();
@@ -203,14 +200,13 @@ int main(int argc, char **argv) {
203200 const auto result = options.parse (argc, argv);
204201
205202 if (result[" help" ].as <bool >()) {
206- std::cout << options.help () << std::endl ;
203+ fmt::print ( " {} \n " , options.help ()) ;
207204 return EXIT_SUCCESS;
208205 }
209206 const size_t repeat = result[" repeat" ].as <size_t >();
210207 const bool single = result[" single" ].as <bool >();
211208 std::vector<std::string> filter = result[" algo-filter" ].as <std::vector<std::string>>();
212- std::cout << " number type: binary"
213- << (single ? " 32 (float)" : " 64 (double)" ) << std::endl;
209+ fmt::println (" number type: binary{}" , (single ? " 32 (float)" : " 64 (double)" ));
214210
215211 std::variant<std::vector<float >, std::vector<double >> numbers;
216212 const auto filename = result[" file" ].as <std::string>();
@@ -221,9 +217,7 @@ int main(int argc, char **argv) {
221217 numbers = get_random_numbers<float >(volume, model);
222218 else
223219 numbers = get_random_numbers<double >(volume, model);
224- std::cout << " # You can also provide a filename (with the -f flag): "
225- " it should contain one string per line corresponding to a number"
226- << std::endl;
220+ fmt::println (" # You can also provide a filename (with the -f flag): it should contain one string per line corresponding to a number" );
227221 }
228222 else {
229223 if (single)
@@ -241,7 +235,7 @@ int main(int argc, char **argv) {
241235 algorithms = Benchmarks::initArgs<double >(errol);
242236
243237 if (repeat > 0 ) {
244- std::cout << " # forcing repeat count to " << repeat << std::endl ;
238+ fmt::println ( " # forcing repeat count to {} " , repeat) ;
245239 std::visit ([repeat](auto &args) {
246240 for (auto &arg : args)
247241 arg.testRepeat = repeat;
@@ -260,7 +254,20 @@ int main(int argc, char **argv) {
260254 }
261255 }, numbers, algorithms);
262256 } catch (const std::exception &e) {
263- std::cout << " error parsing options: " << e.what () << std::endl;
257+ fmt::println (" Error parsing options: {}" , e.what ());
258+ fmt::println (" \n USAGE GUIDE:" );
259+ fmt::println (" ./benchmark [OPTIONS]" );
260+ fmt::println (" \n COMMAND SUMMARY:" );
261+ fmt::println (" The benchmark tool evaluates the performance of different floating-point to string" );
262+ fmt::println (" conversion algorithms. It can use either synthetic data or a file containing" );
263+ fmt::println (" floating-point numbers (one per line)." );
264+ fmt::println (" \n EXAMPLES:" );
265+ fmt::println (" ./benchmark --single # Run benchmark with single precision (float)" );
266+ fmt::println (" ./benchmark --file=data/canada.txt # Run benchmark using numbers from a file" );
267+ fmt::println (" ./benchmark --test # Test correctness instead of performance" );
268+ fmt::println (" ./benchmark --volume=1000 --model=uniform # Generate 1000 uniform random numbers" );
269+ fmt::println (" ./benchmark --algo-filter=ryu,grisu # Only test algorithms containing 'ryu' or 'grisu'" );
270+ fmt::println (" \n For full options list, run: ./benchmark --help" );
264271 return EXIT_FAILURE;
265272 }
266273}
0 commit comments