小编J.k*_*kol的帖子

C++:将字节转换为unsigned int的最快方法

我有一个bytes(unsigned char *)数组,必须转换为整数.整数表示超过三个字节.这就是我所做的

//bytes array is allocated and filled
//allocating space for intBuffer (uint32_t)
unsigned long i = 0;
uint32_t number;
for(; i<size_tot; i+=3){
    uint32_t number = (bytes[i]<<16) | (bytes[i+1]<<8) | bytes[i+2];
    intBuffer[number]++;
}
Run Code Online (Sandbox Code Playgroud)

这段代码很好地完成了它的工作,但是由于内存中的三次访问(特别是对于大的值size_tot,按顺序3000000),它非常慢.有没有办法更快地完成它并提高性能?

c++ performance byte memory-bandwidth

3
推荐指数
1
解决办法
796
查看次数

标签 统计

byte ×1

c++ ×1

memory-bandwidth ×1

performance ×1