home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,382 of 243,242   
   Bonita Montero to All   
   Re: is_binary_file()   
   09 Dec 25 15:09:11   
   
   From: Bonita.Montero@gmail.com   
      
   I made a little benchmark that compares the table code against the   
   convention   
   &&-cascaded code. On my Zen4-PC the table code is about 25% faster with   
   clang.   
   I'm doing a AVX2 and AVX-512 version now. I guess it's about 20 - 30 times   
   faster.   
      
   #include    
      
   #include    
   #include    
   #include    
   #include    
      
   using namespace std;   
   using namespace filesystem;   
   using namespace chrono;   
      
   template   
   bool binary( string const &buf );   
      
   int main()   
   {   
        ifstream ifs;   
        ifs.exceptions( ios_base::failbit | ios_base::badbit );   
        ifs.open( "main.cpp", ios_base::binary | ios_base::ate );   
        streampos pos = ifs.tellg();   
        if( pos > (size_t)-1 )   
            throw ios_base::failure( "file too large", error_code(   
   (int)errc::file_too_large, generic_category() ) );   
        string buf( (size_t)pos, 0 );   
        ifs.seekg( 0 );   
        ifs.read( buf.data(), buf.size() );   
        binary( buf );   
        auto bench = [&]( bool_constant ) -> int   
        {   
            int ret = 0;   
            auto start = high_resolution_clock::now();   
            for( size_t r = 1'000'000; r; --r )   
                ret += binary
( buf );                double secs = (double)duration_cast(       high_resolution_clock::now() - start ).count() / 1.0e9;                cout << (Table ? "table" : "check") << ": " << secs << endl;                return ret;            };            int ret = bench( false_type() );            ret += bench( true_type() );            return ret;       }              template       bool binary( string const &buf )       {            static auto invalid = []( unsigned char c ) static { return c <       0x20 && c != '\r' && c != '\n' && c != '\t'; };            if constexpr( Table )            {                static vector invalidTbl = Table ? []()                    {                        vector ret( numeric_limits::max() );                        for( size_t c = ret.size(); c--; )                            ret[c] = invalid( (unsigned char)c );                        return ret;                    }() : vector();                return find_if( buf.begin(), buf.end(), [&]( unsigned char c )       { return invalidTbl[c]; } ) == buf.end();            }            else                return find_if( buf.begin(), buf.end(), invalid ) == buf.end();       }              --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca