Here is a part of my C++ code where I have problems:
std::bitset<64>a;
std::bitset<64>b;
std::bitset<64>c;
int bit_count=0;
std::vector<int> vec(SIZE,0);
for (i=1;i<NUM;i++)
{
// I do here some operations on a and b (a and b will have bits that are set)
c=a^b;
bit_count=(int) c.count(); // LINE 1
vec[i]=bit_count; // LINE2 2
}
My problem is the following:
- if I comment LINE 1 and LINE 2 the code runs in approx. 109ms;
- if I only comment LINE2 the code runs approx. in 115 ms;
- if I comment LINE 1 and bit_count=0 the code runs approx 130 ms;
- if both lines (LINE 1 and 2) are not commented, the code runs in approx. 350 ms.
Why is the code slow when I use LINE1 and LINE2? I do not find any acceptable explanation.
Note that I also tried vec.push_back(bit_count), and it is also slow. I also tried different cast operations without success.