$ time ./rtoa-pgatram-allinone3 t1.txt t1.txt t1.txt t1.txt >f4.tmp not using fast_io do_it_all time : 1.936 secs real 0m1.953s user 0m1.832s sys 0m0.121s $ cmp f4.tmp fixed4.tmp #### $ time ./rtoa-pgatram-allinone3 t1.txt t1.txt t1.txt t1.txt >f4.tmp using fast_io do_it_all time : 0.875 secs real 0m0.894s user 0m0.803s sys 0m0.091s $ cmp f4.tmp fixed4.tmp #### $ time ./rtoa-pgatram-allinone t1.txt t1.txt t1.txt t1.txt >f4.tmp do_it_all time : 1.034 secs real 0m1.049s user 0m0.988s sys 0m0.061s #### // rtoa-pgatram-allinone3.cpp. Crude allinone version. // based on rtoa-pgatram-allinone2.cpp https://perlmonks.org/?node_id=11152186 // // Obtain the fast_io library (required dependency): // git clone --depth=1 https://github.com/cppfastio/fast_io // // Compile with g++ or clang++: // g++ -o rtoa-pgatram-allinone3 -std=c++20 -Wall -O3 -I fast_io/include rtoa-pgatram-allinone3.cpp // Uncomment to use the fast_io C++ library #define USE_FAST_IO_L 1 #include #include #include #include #include #include #include #include #ifdef USE_FAST_IO_L // See [id://11149504] for more info on the fast_io C++ library #include #include #else #include #include #endif // --------------------------------------------------------------- typedef std::chrono::high_resolution_clock high_resolution_clock; typedef std::chrono::high_resolution_clock::time_point time_point; typedef std::chrono::milliseconds milliseconds; double elaspe_time( time_point cend, time_point cstart) { return double ( std::chrono::duration_cast(cend - cstart).count() ) * 1e-3; } // --------------------------------------------------------------- // Though there are less than 256 initializers in this ascii table, // the others are guaranteed by ANSI C to be initialized to zero. static const int romtab[256] = { 0,0,0,0,0,0, 0, 0, 0, 0, // 00- 09 0,0,0,0,0,0, 0, 0, 0, 0, // 10- 19 0,0,0,0,0,0, 0, 0, 0, 0, // 20- 29 0,0,0,0,0,0, 0, 0, 0, 0, // 30- 39 0,0,0,0,0,0, 0, 0, 0, 0, // 40- 49 0,0,0,0,0,0, 0, 0, 0, 0, // 50- 59 0,0,0,0,0,0, 0, 100, 500, 0, // 60- 69 0,0,0,1,0,0, 50,1000, 0, 0, // 70- 79 0,0,0,0,0,0, 5, 0, 10, 0, // 80- 89 0,0,0,0,0,0, 0, 0, 0, 100, // 90- 99 500,0,0,0,0,1, 0, 0, 50,1000, // 100-109 0,0,0,0,0,0, 0, 0, 5, 0, // 110-119 10,0,0,0,0,0, 0, 0, 0, 0 // 120-129 }; // Return the arabic number for a roman letter c. // Return zero if the roman letter c is invalid. inline int urtoa(int c) { return romtab[c]; } inline int accfn(int t, char c) { return t + urtoa(c) - t % urtoa(c) * 2; } inline int roman_to_dec(std::string_view st) { return std::accumulate(st.begin(), st.end(), 0, accfn); } #ifdef USE_FAST_IO_L // Read an input file of Roman Numerals and do it all (fast_io version) static void do_it_all( std::string_view fname // in: file name containing a list of Roman Numerals ) { try { fast_io::filebuf_file fbf(fname, fast_io::open_mode::in); for (std::string line; fast_io::io::scan(fbf, fast_io::mnp::line_get(line)); ) { fast_io::io::println(roman_to_dec(line)); } } catch (fast_io::error e) { fast_io::io::perrln("Error opening '", fname, "' : ", e); }; } #else // Read an input file of Roman Numerals and do it all (std C++ version) static void do_it_all( const char* fname // in: file name containing a list of Roman Numerals ) { std::ifstream infile(fname); if (!infile.is_open()) { std::cerr << "Error opening '" << fname << "'\n"; return; } for (std::string line; std::getline(infile, line); ) { std::cout << roman_to_dec(line) << "\n"; } infile.close(); } #endif int main(int argc, char* argv[]) { if (argc < 2) { if (argc > 0) std::cerr << "usage: rtoa-pgatram-allinone3 file... >out.txt\n"; return 1; } #ifdef USE_FAST_IO_L std::cerr << "using fast_io\n"; #else std::cerr << "not using fast_io\n"; #endif // Get the list of input files from the command line int nfiles = argc - 1; char** fname = &argv[1]; std::cerr << std::setprecision(3) << std::setiosflags(std::ios::fixed); time_point cstartall, cendall; cstartall = high_resolution_clock::now(); for (int i = 0; i < nfiles; ++i) do_it_all( fname[i] ); cendall = high_resolution_clock::now(); double ctakenall = elaspe_time(cendall, cstartall); std::cerr << "do_it_all time : " << std::setw(8) << ctakenall << " secs\n"; return 0; }