I have a class that contains two bitmaps.
class SpaceArray
{
static const uint RES = 16;
static const uint ARRAYSIZE = RES * RES * RES * RES * RES;
std::bitset<ARRAYSIZE> deadly;
std::bitset<ARRAYSIZE> sure;
};
When I encapsulate them, performance is impacted, even though logically, the two programs are the exact same.
#pragma once
#include <bitset>
template <unsigned int SIZE>
class DualBitArray
{
typedef unsigned int uint;
static const uint BITSETSIZE = SIZE * SIZE * SIZE * SIZE * SIZE;
std::bitset<BITSETSIZE> deadly;
std::bitset<BITSETSIZE> fixed;
public:
bool Valid(uint i) const {return i<BITSETSIZE;}
bool IsDeadly(uint i) const {return deadly.test(i);}
bool IsFixed(uint i) const {return fixed.test(i);}
uint NumSet() const {return fixed.count();}
void SetDeadly(uint i, bool val) {deadly.set(i, val);}
void SetFixed(uint i, bool val){fixed.set(i, val);}
};
class SpaceArray
{
static const uint RES = 16;
DualBitArray<RES> mydata;
};
Why is the second version slower? I compiled on gcc with -O2, so I assumed that the two would be optimized into the same code. What went wrong?