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

后端 未结 20 2322
孤独总比滥情好
孤独总比滥情好 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:54

    int is_empty(char * buf, int size)
    {
       return buf[0] == '\0';
    }
    

    If your buffer is not a character string, I think that's the fastest way to check...

    memcmp() would require you to create a buffer the same size and then use memset to set it all as 0. I doubt that would be faster...

提交回复
热议问题