in reply to Re^5: Risque Romantic Rosetta Roman Race - MCE Results on AMD Box
in thread Risque Romantic Rosetta Roman Race
I may have been overthinking this. :) Here's a simple all-in-one version with no interim storage in vectors at all.
// rtoa-pgatram-allinone.cpp. Crude allinone version. // Compile with: // g++ -o rtoa-pgatram-allinone -std=c++20 -Wall -O3 rtoa-pgatram-al +linone.cpp // or: // clang++ -o rtoa-pgatram-allinone -std=c++20 -Wall -O3 rtoa-pgatra +m-allinone.cpp // or: // g++ -o rtoa-pgatram-allinone -std=c++20 -Wall -O3 -I "$HOME/local +-fast_io/fast_io/include" rtoa-pgatram-allinone.cpp // to use the locally installed fast_io header-only library #include <cctype> #include <cstring> #include <string> // #include <vector> #include <numeric> #include <chrono> #include <thread> #include <iostream> #include <iomanip> // See [id://11149504] for more info on the fast_io C++ library #include <fast_io.h> // --------------------------------------------------------------- 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<milliseconds>(cend - cstart).count() ) * 1e-3; } // --------------------------------------------------------------- // Longest roman numeral is MMMDCCCLXXXVIII (3888) of length 15 // XXX: I'm off by one somewhere because 3888 fails with // MAX_STR_LEN_L of 16 but works with 17 #define MAX_STR_LEN_L 17 // The basic idea is to keep this struct small and without pointers to // improve data locality/cache performance when traversing the vector struct str_type { char slen; char str[MAX_STR_LEN_L]; }; // using vec_str_type = std::vector<str_type>; // using vec_int_type = std::vector<int>; // --------------------------------------------------------------- // 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(const str_type& st) { return std::accumulate( st.str, st.str + st.slen, 0, accfn ); } // Read an input file of Roman Numerals and do it all static void do_it_all( const char* fname // in: file name containing a list of Roma +n Numerals ) { FILE* fh; str_type line; fh = ::fopen(fname, "r"); if ( fh == NULL ) { std::cerr << "Error opening '" << fname << "' : " << strerror(er +rno) << "\n"; return; } while ( ::fgets( line.str, MAX_STR_LEN_L, fh ) != NULL ) { line.slen = ::strlen(line.str) - 1; // -1 to strip trailing n +ewline // std::cout << roman_to_dec(line) << '\n'; fast_io::io::println( roman_to_dec(line) ); } ::fclose(fh); } int main(int argc, char* argv[]) { if (argc < 2) { std::cerr << "usage: rtoa-pgatram-allinone file...\n"; return 1; } // 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::fix +ed); 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; }
$ 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 $ cmp f4.tmp fixed4.tmp $ time ./rtoa-pgatram-allinone t1.txt t1.txt t1.txt t1.txt >f4.tmp do_it_all time : 1.047 secs real 0m1.070s user 0m0.989s sys 0m0.081s $ cmp f4.tmp fixed4.tmp
As you can see, this is twice as fast as rtoa-pgatram-fixed.
$ time ./rtoa-pgatram-fixed t1.txt t1.txt t1.txt t1.txt >f4.tmp read_input_files : 15996000 items read file time : 0.759 secs roman_to_dec time : 0.367 secs output time : 1.032 secs total time : 2.160 secs real 0m2.179s user 0m1.908s sys 0m0.270s $ cmp f4.tmp fixed4.tmp
Update: Oops, the above rtoa-pgatram-fixed timing figures were built without using fast_io. The timings with fastio on my machine are:
... not twice as fast, but it's faster when you don't store anything in a vector ... though rtoa-pgatram-openmp might be faster with many files ... so I probably need to find a way to make rtoa-pgatram-allinone concurrent somehow (e.g. via chunking).read_input_files : 15996000 items read file time : 0.750 secs roman_to_dec time : 0.370 secs output time : 0.389 secs total time : 1.510 secs real 0m1.529s user 0m1.348s sys 0m0.181s
Will this all in one version rtoa-pgatram-allinone be deemed acceptable by marioroy?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Risque Romantic Rosetta Roman Race - All in One
by marioroy (Prior) on May 15, 2023 at 12:23 UTC | |
by eyepopslikeamosquito (Archbishop) on May 16, 2023 at 00:36 UTC | |
by marioroy (Prior) on May 16, 2023 at 02:05 UTC | |
by marioroy (Prior) on May 16, 2023 at 13:39 UTC | |
by marioroy (Prior) on May 19, 2023 at 13:31 UTC | |
|