Concatenate boost::dynamic_bitset or std::bitset

后端 未结 5 1961
梦毁少年i
梦毁少年i 2020-12-16 19:57

what is the best way to concatenate 2 bitsets?

For example i\'ve got

boost::dynamic_bitset<> test1( std::string(\"1111\") );
boost::dynamic_bit         


        
5条回答
  •  隐瞒了意图╮
    2020-12-16 20:53

    I've tested several solutions and it seems that :

    • a good old "for loop" is the fastest
    • bitset is a lot faster than dynamic_bitset (not surprising), if no memory allocation is needed the overhead is lower but still exists.
    • It may seems obvious but, directly append a bitset to another without creating a new one is a faster. This solution is not suitable if you need to keep the first bitset unchanged (obvious too).
    • The 3 solutions don't produces the same result, you have to do some tuning depending on want you want(see below).

    Here is my test code :

    #include 
    #include 
    #include 
    #include "scul/PreciseTimer.h"
    
    boost::dynamic_bitset<> concatOperatorsDyn( const boost::dynamic_bitset<>& bs1,const boost::dynamic_bitset<>& bs2)
    {
        boost::dynamic_bitset<> bs1Copy(bs1);
        boost::dynamic_bitset<> bs2Copy(bs2);
        size_t totalSize=bs1.size()+bs2.size();
        bs1Copy.resize(totalSize);
        bs2Copy.resize(totalSize);
        bs1Copy<<=bs2.size();
        bs1Copy|=bs2Copy;
        return bs1Copy;
    }
    
    template
    std::bitset concatString( const std::bitset& bs1,const std::bitset& bs2)
    {
        std::string s1=bs1.to_string,std::allocator >();
        std::string s2=bs2.to_string,std::allocator >();
    
        std::bitset res(s1+s2);
        return res;
    }
    
    template
    std::bitset concatLoop( const std::bitset& bs1,const std::bitset& bs2)
    {
        std::bitset res;
        for(size_t i=0;i concatLoopDyn( const boost::dynamic_bitset<>& bs1,const boost::dynamic_bitset<>& bs2)
    {
    boost::dynamic_bitset<> res(bs1);
    res.resize(bs1.size()+bs2.size());
        size_t bs1Size=bs1.size();
        size_t bs2Size=bs2.size();
    
        for(size_t i=0;i concatStringDyn( const boost::dynamic_bitset<>& bs1,const boost::dynamic_bitset<>& bs2)
    {
        std::string s1;
        std::string s2;
        to_string(bs1,s1);
        to_string(bs2,s2);
    
        boost::dynamic_bitset<> res(s1+s2);
        return res;
    }
    
    
    template
    void injectLoop( std::bitset& bs1,const std::bitset& bs2,int start=s1-s2)
    {
        for(size_t i=0;i& bs1,const boost::dynamic_bitset<>& bs2,int start)
    {
        for(size_t i=0;i bs1(std::string("11111111110000000000"));
        std::bitset<30> bs1Bis(std::string("11111111110000000000"));
        const std::bitset<10> bs2(std::string("0000011111"));
        std::bitset<30> bs3;
    
    
        const boost::dynamic_bitset<> bs1D(std::string("11111111110000000000"));
        boost::dynamic_bitset<> bs1DBis(std::string("11111111110000000000"));
        bs1DBis.resize(30);
        const boost::dynamic_bitset<> bs2D(std::string("0000011111"));
        boost::dynamic_bitset<> bs3D;
    
        scul::PreciseTimer t;
        double d=0.;
    
        int nbIter=100;
    
        std::cout<<"Bitset concat with strings"<(bs1,bs2);
        d=t.stop();
        std::cout<,std::allocator >()<(bs1,bs2);
        d=t.stop();
        std::cout<,std::allocator >()<(bs1Bis,bs2);
        d=t.stop();
        std::cout<,std::allocator >()<
        

提交回复
热议问题