// llil3m.cpp. C++ 11 version of Perl llil.pl. // C++ version inspired by Mario's famous dualvar two-sort Perl solution. // g++ compile on Linux: // g++ -o llil3m -std=c++20 -Wall -O3 llil3m.cpp // g++ -o llil3m -std=c++20 -fopenmp -Wall -O3 llil3m.cpp // This g++ command also works with mingw C++ compiler (https://sourceforge.net/projects/mingw-w64) // that comes bundled with Strawberry Perl (C:\Strawberry\c\bin\g++.exe). // Example run: llil3m tt1.txt tt2.txt tt3.txt >out.txt // Uncomment next line to mimic marioroy two sort trick (needs stable sort) #define MARIOROY_TWO_SORT_TRICK_L 1 #include #include #include #include #include #include #include #include #include #include #include #include #include #if defined(_OPENMP) #include #include #else #include #include #endif #include #include #include static_assert(sizeof(size_t) == sizeof(int64_t), "size_t too small, need a 64-bit compile"); // ---------------------------------------------------------------------------- // Crude hack to see Windows Private Bytes in Task Manager by sleeping at // program end (see also sleep hack at end of main) // #include // #include // For some performance hacks to speed up C++ I/O see: // https://www.reddit.com/r/rust/comments/9xedap/how_to_achieve_fast_stdinstdout_io_suitable_for/ // The only one we use here is to prefer "\n" to std::endl to reduce stdout flushing // ---------------------------------------------------------------------------- typedef long long llil_int_type; // Note: all words in big1.txt, big2.txt, big3.txt are <= 6 chars in length // To use (limited length) fixed length strings uncomment the next line // big.txt max word length is 6 // long.txt max word length is 208 // Based on rough benchmarking, this short fixed string hack // is only worth trying for MAX_STR_LEN_L up to about 64 #define MAX_STR_LEN_L 6 #ifdef MAX_STR_LEN_L using str_type = std::array; #else using str_type = std::string; #endif using str_int_type = std::pair; using int_str_type = std::pair; using vec_str_int_type = std::vector; using vec_int_str_type = std::vector; using set_int_str_type = std::set; using map_str_int_type = std::map; // Mimic the Perl get_properties subroutine ---------------------------- // Limit line length and use lower level ANSI C functions to try to boost I/O performance #define MAX_LINE_LEN_L 255 static void get_properties( int nfiles, // in: the number of input files char* fname[], // in: the input file names map_str_int_type& hash_ret) // out: a hash of properties { FILE* fh; char line[MAX_LINE_LEN_L+1]; char* found; llil_int_type count; for (int i = 0; i < nfiles; ++i) { fh = ::fopen(fname[i], "r"); if (fh == NULL) { std::cerr << "Error opening '" << fname[i] << "' : errno=" << errno << "\n"; continue; } while ( ::fgets(line, MAX_LINE_LEN_L, fh) != NULL ) { found = ::strchr(line, '\t'); count = ::atoll( &line[found - line + 1] ); line[found - line] = '\0'; // word #ifdef MAX_STR_LEN_L str_type fixword { { '\0', '\0', '\0', '\0', '\0', '\0' } }; ::memcpy( fixword.data(), line, found - line ); hash_ret[fixword] -= count; #else hash_ret[line] -= count; #endif } ::fclose(fh); } } // --------------------------------------------------------------------- int main(int argc, char* argv[]) { if (argc < 2) { std::cerr << "usage: llil3m file1 file2 ... >out.txt\n"; return 1; } #ifdef MAX_STR_LEN_L std::cerr << "llil3m (fixed string length=" << MAX_STR_LEN_L << ") start\n"; #else std::cerr << "llil3m start\n"; #endif #if defined(_OPENMP) std::cerr << " - openmp version\n"; #endif time_t tstart1 = ::time(NULL); clock_t cstart1 = ::clock(); // Create the hash of properties map_str_int_type hash_ret; get_properties(argc - 1, &argv[1], hash_ret); clock_t cend1 = ::clock(); double ctaken1 = (double) (cend1 - cstart1) / (double)CLOCKS_PER_SEC; std::cerr << "get_properties CPU time : " << ctaken1 << " secs\n"; clock_t cstart2 = ::clock(); // Create a vector to sort (can't sort a std::map directly) vec_str_int_type v( hash_ret.begin(), hash_ret.end() ); clock_t cend2v = ::clock(); #ifdef MARIOROY_TWO_SORT_TRICK_L // std::map is already sorted by string key so just stable_sort by value #if defined(_OPENMP) __gnu_parallel::stable_sort( #else std::stable_sort( #endif v.begin(), v.end(), [](const str_int_type& left, const str_int_type& right) { return left.second < right.second; } ); #else // Sort descending by value, i.e. mimic this Perl code in C++: // sort { $href->{$b} <=> $href->{$a} || $a cmp $b } keys %{$href} // No surprise that string key comparison is way slower than numeric key comparison in std::sort() #if defined(_OPENMP) __gnu_parallel::sort( #else std::sort( #endif v.begin(), v.end(), [](const str_int_type& left, const str_int_type& right) { return left.second != right.second ? left.second < right.second : left.first < right.first; } ); #endif clock_t cend2s = ::clock(); #ifdef MAX_STR_LEN_L // If name is not NULL-terminated: for ( auto const& n : v ) ::printf( "%.*s\t%lld\n", MAX_STR_LEN_L, n.first.data(), -n.second ); // If name is NULL-terminated: // for ( auto const& n : v ) ::printf( "%s\t%lld\n", n.first.data(), -n.second ); // for ( auto const& n : v ) std::cout << n.first.data() << '\t' << -n.second << '\n'; #else // Can try printf vs std::cout to see which is faster for ( auto const& n : v ) ::printf( "%s\t%lld\n", n.first.c_str(), -n.second ); // for ( auto const& n : v ) std::cout << n.first << '\t' << -n.second << '\n'; #endif clock_t cend2 = ::clock(); time_t tend2 = ::time(NULL); long ttaken = static_cast(::difftime(tend2, tstart1) + 0.5); double ctaken = (double) (cend2 - cstart1) / (double)CLOCKS_PER_SEC; double ctaken2v = (double) (cend2v - cstart2) / (double)CLOCKS_PER_SEC; double ctaken2s = (double) (cend2s - cend2v) / (double)CLOCKS_PER_SEC; double ctaken2o = (double) (cend2 - cend2s) / (double)CLOCKS_PER_SEC; std::cerr << "vector copy CPU time : " << ctaken2v << " secs\n"; #ifdef MARIOROY_TWO_SORT_TRICK_L std::cerr << "vector stable sort CPU time : " << ctaken2s << " secs\n"; #else std::cerr << "vector sort CPU time : " << ctaken2s << " secs\n"; #endif std::cerr << "write stdout CPU time : " << ctaken2o << " secs\n"; std::cerr << "total CPU time : " << ctaken << " secs\n"; std::cerr << "total wall clock : " << ttaken << " secs\n"; // Hack to see Private Bytes in Windows Task Manager (uncomment next line so process doesn't exit too quickly) // std::this_thread::sleep_for(std::chrono::milliseconds(90000000)); return 0; }