C++ 读取位性能
我正在编写一个助手类,我打算用它从数据块中反向读取位 我尝试进行优化,使用“rol”指令屏蔽数据。然而,令我惊讶的是,这实际上比在每次访问期间创建一个新的位掩码要慢C++ 读取位性能,c++,performance,bit-manipulation,C++,Performance,Bit Manipulation,我正在编写一个助手类,我打算用它从数据块中反向读取位 我尝试进行优化,使用“rol”指令屏蔽数据。然而,令我惊讶的是,这实际上比在每次访问期间创建一个新的位掩码要慢 class reverse_bit_reader { public: static const size_t bits_per_block = sizeof(unsigned long)*8; static const size_t high_bit = 1 << (bits_per_block-1);
class reverse_bit_reader
{
public:
static const size_t bits_per_block = sizeof(unsigned long)*8;
static const size_t high_bit = 1 << (bits_per_block-1);
reverse_bit_reader(const void* data, size_t size)
: data_(reinterpret_cast<const unsigned long*>(data))
, index_(size-1)
{
// Bits are stored in left to right order, potentially ignore the last bits
size_t last_bit_index = index_ % bits_per_block;
bit_mask_ = high_bit >> (last_bit_index+1);
if(bit_mask_ == 0)
bit_mask_ = high_bit;
}
bool next_bit1()
{
return get_bit(index_--);
}
bool next_bit2() // Why is next_bit1 faster?
{
__asm // Rotate bit_mask.
{
mov eax, [ecx+0];
rol eax, 1;
mov [ecx+0], eax;
}
return data_[index_-- / bits_per_block] & bit_mask_;
}
bool eof() const{return index_ < 0;}
private:
bool get_bit(size_t index) const
{
const size_t block_index = index / bits_per_block;
const size_t bit_index = index % bits_per_block;
const size_t bit_mask = high_bit >> bit_index;
return data_[block_index] & bit_mask;
}
unsigned long bit_mask_;
int index_;
const unsigned long* data_;
};
类反向位读取器
{
公众:
静态常量大小\u t位\u/块=sizeof(无符号长)*8;
静态常量大小\u t高位\u=1>(最后一位\u索引+1);
if(位掩码=0)
位屏蔽位=高位;
}
bool next_bit1()
{
返回get_位(索引_--);
}
bool next\u bit2()//为什么next\u bit1更快?
{
__asm//旋转位掩码。
{
mov-eax[ecx+0];
rol-eax,1;
mov[ecx+0],eax;
}
返回数据\uu[索引\uu-/每个块的位\u]&位\u掩码\uu;
}
bool eof()常量{返回索引{0;}
私人:
布尔获取位(大小索引)常量
{
常量大小块索引=每个块的索引/位;
const size\u t bit\u index=每个块的索引%bits\u;
常量大小\u t位\u掩码=高位>>位索引;
返回数据块索引和位掩码;
}
无符号长位掩码;
int索引;
常量无符号长*数据;
};
有人能解释为什么下一个比特1比下一个比特2快吗?如果要从最重要的比特开始按顺序从long中读取比特,并且希望它尽可能快,您能按照这些思路做些什么吗
#define GETBIT ((theBit = (theLong < 0)), (theLong <<= 1), theBit)
#define GETBIT((位=(长<0)),(长的另一种方式:为什么它应该更快?你正在加载掩码,旋转1,然后再次存储它。这应该比将寄存器设置为1,并将其按“索引%bits\u per\u块”计算的值进行移位要慢,后者被转换为简单的“and”不管怎样,指令。如果你想确切地知道发生了什么,研究生成的目标代码。因为编译器在生成好代码方面比你更好。相信它。行1…而且,当你实际处理位时,这里发生的事情可能是无关紧要的。