- Sieve of Eratosthenes - 4 Updates
- What is visitor pattern? - 3 Updates
Bonita Montero <Bonita.Montero@gmail.com>: Aug 30 01:01PM +0200 I implemented Alf's code according to my taste. The code has about 40% the performance of my code if I don't output a file with the numbers on Sklyake CPU with g++ 12 and -march=native. #include <iostream> #include <vector> #include <charconv> #include <fstream> #include <cmath> #include <cstring> using namespace std; int main( int argc, char **argv ) { constexpr size_t BUF_SIZE = 0x100000; size_t end; if( argc < 2 ) return EXIT_FAILURE; char const *sizeStr = argv[1]; bool hex = sizeStr[0] == '0' && (sizeStr[1] == 'x' || sizeStr[1] == 'X'); sizeStr += 2 * hex; if( from_chars_result fcr = from_chars( sizeStr, sizeStr + strlen( sizeStr ), end, !hex ? 10 : 16 ); (bool)fcr.ec || *fcr.ptr ) return EXIT_FAILURE; if( !++end ) throw bad_alloc(); vector<bool> primes( end, true ); ofstream ofs; string printBuf; if( argc < 3 || *argv[2] ) ofs.exceptions( ofstream::failbit | ofstream::badbit ), ofs.open( argc >= 3 ? argv[2] : "primes.txt", ofstream::trunc ), printBuf.reserve( BUF_SIZE + 31 ); size_t sqr = (ptrdiff_t)ceil( sqrt( (ptrdiff_t)end ) ); for( size_t p = 2; p < end; ++p ) { while( !primes[p] ) if( ++p == end ) goto end; if( p < sqr ) { size_t m = p * p; do primes[m] = false; while( (m += p) < end ); } if( !ofs.is_open() ) continue; char strPrime[32]; auto [ptr, ec] = to_chars( strPrime, std::end( strPrime ), p ); if( (bool)ec ) [[unlikely]] throw system_error( (int)ec, generic_category(), "converson failed" ); *ptr++ = '\n'; printBuf += string_view( strPrime, ptr ); if( printBuf.size() >= BUF_SIZE ) ofs << printBuf, printBuf.resize( 0 ); } end: if( ofs.is_open() ) ofs << printBuf; } |
Muttley@dastardlyhq.com: Aug 30 03:38PM On Wed, 30 Aug 2023 13:01:16 +0200 > ofs.exceptions( ofstream::failbit | ofstream::badbit ), > ofs.open( argc >= 3 ? argv[2] : "primes.txt", ofstream::trunc ), > printBuf.reserve( BUF_SIZE + 31 ); Commas as statement seperators instead of using a bracketed block? Are you posting this code for lols? |
Bonita Montero <Bonita.Montero@gmail.com>: Aug 30 06:31PM +0200 >> printBuf.reserve( BUF_SIZE + 31 ); > Commas as statement seperators instead of using a bracketed block? > Are you posting this code for lols? I often concatenate a small number of statements with the comma-operator to save screen space. I never had any issues with that. That's just a matter of personal taste. If you're noticing this until now, I'm going to assume that you haven't really looked at the previous code. But that's just the syntax issue. My code is also superior in terms of safety (iterator debuggin) and performance (+2/3 more performance with g++ 12 and a Sklake-CPU). |
Bonita Montero <Bonita.Montero@gmail.com>: Aug 30 07:02PM +0200 Am 29.08.2023 um 20:34 schrieb Bonita Montero: > size_t g = countr_zero( pSieve->w & (word_t)-1 << p % BITS ); size_t g = countr_zero( (word_t)(pSieve->w & (word_t)-1 << p % BITS) ); OMG, an integral promotion error. If word_t is defined < unsigned the results are wrong. |
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>: Aug 29 09:44PM -0400 > functional programming. Its more than just no cost recursion , its also > polymorphism by *VALUE*. That is not and probably never will be possible in > C++. Hard to believe but they were thinking of it as recently as Sep 2020, see https://wg21.link/p1371r3 But of course, on mutable objects, pattern mathcing loses some 90% of its use. |
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>: Aug 29 10:14PM -0400 Bonita Montero wrote: >> solve a valid visitor problem. > Either I didn't describe the difference to what you imagine correctly > or you just didn't understand me. None of the above it is just you are not trying to read. > needs a base class with a virtual method to derive from, and the derived > object is passed by reference to the Visitor function of the objects > being visited. No. Visitor needs no types. The **problems that Visitor solves** include data structures of multiple types. The applicability of Visitor is exactly when the programmer is limited to small backward-compatible changes to the type hierarchy **given in the problem, not "needed by Visitor"**, ideally, no changes at all. Try to read what's written, not what you want to see. I will try one more time, in negative terms, in case it makes it easier for you to comprehend: No type hierarchy in the problem -- no need in Visitor in the solution. Now, "a base class with a virtual method to derive from" are just possible implementation details for the type hierarchy, so not 100% needed. The type hierarchy could be a variant or another type hierarchy where the dynamic type is detectable at run-time. There must be, however, a comprehensive type hierarchy for the Visitor to be useful. What you are trying to demonstrate, on the other hand, is called a "higher-order function" in functional programming. And, no, it does not need a new name because it already has an industry-standard name. |
Bonita Montero <Bonita.Montero@gmail.com>: Aug 30 06:13AM +0200 Am 30.08.2023 um 04:14 schrieb Pavel: > exactly when the programmer is limited to small backward-compatible > changes to the type hierarchy **given in the problem, not "needed by > Visitor"**, ideally, no changes at all. You simply don't understand what I wrote. |
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page. To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com. |
No comments:
Post a Comment