How to check whether all bytes in a memory block are zero

后端 未结 10 709
渐次进展
渐次进展 2021-02-03 21:05

I have a block of memory with elements of fixed size, say 100 bytes, put into it one after another, all with the same fixed length, so memory looks like this

&l         


        
10条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 21:55

    I can't recall a standard library function which could do this for you. If you are not sure this causes any performance issues I'd just use the loop, maybe replace char* with int* as already suggested.

    If you do have to optimize you could unroll the loop:

    bool allZeroes(char* buffer)
    {
        int* p = (int*)buffer;   // you better make sure your block starts on int boundary
        int acc = *p;
        acc |= *++p;
        acc |= *++p;
        ...
        acc |= *++p;    // as many times as needed
    
        return acc == 0;
    }
    

    You may need to add special handling for the end of buffer if it's size is not a multiple of sizeof(int), but it could be more efficient to allocate a slightly larger block with some padding bytes set to 0.

    If your blocks are large you could treat them as a sequence of smaller blocks and loop over them, using the code above for each small block.

    I would be curious to know how this solution compares with std::upper_bound(begin,end,0) and memcmp.

    EDIT

    Did a quick check how a home-grown implementation compares with memcmp, used VS2010 for that.

    In short:

    1) in debug mode home-grown can be twice as fast as memcmp

    2) in release with full optimization memcmp has an edge on the blocks which start with non-0s. As the length of the zero-filled preamble increases it starts losing, then somehow magically gets almost as fast as homegrown, about only 10% slower.

    So depending on your data patterns and need/desire to optimize you could get some extra performance from rolling out your own method, but memcmp is a rather reasonable solution.

    Will put the code and results on github in case you could use them.

提交回复
热议问题