Faster approach to checking for an all-zero buffer in C?

后端 未结 20 2350
孤独总比滥情好
孤独总比滥情好 2020-12-03 05:33

I am searching for a faster method of accomplishing this:

int is_empty(char * buf, int size) 
{
    int i;
    for(i = 0; i < size; i++) {
        if(buf[         


        
20条回答
  •  再見小時候
    2020-12-03 05:48

    One potential way, inspired by Kieveli's dismissed idea:

    int is_empty(char *buf, size_t size)
    {
        static const char zero[999] = { 0 };
        return !memcmp(zero, buf, size > 999 ? 999 : size);
    }
    

    Note that you can't make this solution work for arbitrary sizes. You could do this:

    int is_empty(char *buf, size_t size)
    {
        char *zero = calloc(size);
        int i = memcmp(zero, buf, size);
        free(zero);
        return i;
    }
    

    But any dynamic memory allocation is going to be slower than what you have. The only reason the first solution is faster is because it can use memcmp(), which is going to be hand-optimized in assembly language by the library writers and will be much faster than anything you could code in C.

    EDIT: An optimization no one else has mentioned, based on earlier observations about the "likelyness" of the buffer to be in state X: If a buffer isn't empty, will it more likely not be empty at the beginning or the end? If it's more likely to have cruft at the end, you could start your check at the end and probably see a nice little performance boost.

    EDIT 2: Thanks to Accipitridae in the comments:

    int is_empty(char *buf, size_t size)
    {
        return buf[0] == 0 && !memcmp(buf, buf + 1, size - 1);
    }
    

    This basically compares the buffer to itself, with an initial check to see if the first element is zero. That way, any non-zero elements will cause memcmp() to fail. I don't know how this would compare to using another version, but I do know that it will fail quickly (before we even loop) if the first element is nonzero. If you're more likely to have cruft at the end, change buf[0] to buf[size] to get the same effect.

提交回复
热议问题