What is the performance of std::bitset?

后端 未结 5 451
你的背包
你的背包 2020-12-24 05:24

I recently asked a question on Programmers regarding reasons to use manual bit manipulation of primitive types over std::bitset.

From that discussion I

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 05:42

    Did a short test profiling std::bitset vs bool arrays for sequential and random access - you can too:

    #include 
    #include 
    #include  // rand
    #include  // timer
    
    inline unsigned long get_time_in_ms()
    {
        return (unsigned long)((double(clock()) / CLOCKS_PER_SEC) * 1000);
    }
    
    
    void one_sec_delay()
    {
        unsigned long end_time = get_time_in_ms() + 1000;
    
        while(get_time_in_ms() < end_time)
        {
        }
    }
    
    
    
    int main(int argc, char **argv)
    {
        srand(get_time_in_ms());
    
        using namespace std;
    
        bitset<5000000> bits;
        bool *bools = new bool[5000000];
    
        unsigned long current_time, difference1, difference2;
        double total;
    
        one_sec_delay();
    
        total = 0;
        current_time = get_time_in_ms();
    
        for (unsigned int num = 0; num != 200000000; ++num)
        {
            bools[rand() % 5000000] = rand() % 2;
        }
    
        difference1 = get_time_in_ms() - current_time;
        current_time = get_time_in_ms();
    
        for (unsigned int num2 = 0; num2 != 100; ++num2)
        {
            for (unsigned int num = 0; num != 5000000; ++num)
            {
                total += bools[num];
            }
        }   
    
        difference2 = get_time_in_ms() - current_time;
    
        cout << "Bool:" << endl << "sum total = " << total << ", random access time = " << difference1 << ", sequential access time = " << difference2 << endl << endl;
    
    
        one_sec_delay();
    
        total = 0;
        current_time = get_time_in_ms();
    
        for (unsigned int num = 0; num != 200000000; ++num)
        {
            bits[rand() % 5000000] = rand() % 2;
        }
    
        difference1 = get_time_in_ms() - current_time;
        current_time = get_time_in_ms();
    
        for (unsigned int num2 = 0; num2 != 100; ++num2)
        {
            for (unsigned int num = 0; num != 5000000; ++num)
            {
                total += bits[num];
            }
        }   
    
        difference2 = get_time_in_ms() - current_time;
    
        cout << "Bitset:" << endl << "sum total = " << total << ", random access time = " << difference1 << ", sequential access time = " << difference2 << endl << endl;
    
        delete [] bools;
    
        cin.get();
    
        return 0;
    }
    

    Please note: the outputting of the sum total is necessary so the compiler doesn't optimise out the for loop - which some do if the result of the loop isn't used.

    Under GCC x64 with the following flags: -O2;-Wall;-march=native;-fomit-frame-pointer;-std=c++11; I get the following results:

    Bool array: random access time = 4695, sequential access time = 390

    Bitset: random access time = 5382, sequential access time = 749

提交回复
热议问题