开发者

Can (x==0) be more efficient than (0==x)? [duplicate]

开发者 https://www.devze.com 2023-03-08 20:45 出处:网络
This question already has answers here: 开发者_JS百科 Closed 11 years ago. Possible Duplicate: Which one will execute faster, if(flag==0) or if(0==flag)?
This question already has answers here: 开发者_JS百科 Closed 11 years ago.

Possible Duplicate:

Which one will execute faster, if(flag==0) or if(0==flag)?

I usually write my equality conditions as:

if(0==x)

as many people do, instead of

if(x==0) 

so that the compiler will tell me when I accidentally type = instead of ==.

Someone told me that some compilers implement this as two register loads, instead of one using a not-equal-zero operation, and so it is less efficient.

Anyone know if this is a reasonable comment?


Someone told me that some compilers implement this as two register loads, instead of one using a not-equal-zero operation

There is no technical reason to do this. So no, any compiler worth its salt will not make this irrelevant distinction: since both statements are strictly equivalent, and since this can be trivially recognized by the compiler, it will treat them identically.

Note that this is only true for built-in types and user-defined types with a well-behaved operator ==. Theoretically a user could provide an asymmetric operator == overload where this equivalence isn’t given.


Who knows what "some compilers" do, but in general, no, I wouldn't expect any difference whatsoever in the code generated by a reasonable compiler.


I don't think so. The compiler will interpret that expression as a comparison and the effect will be the same. If the compiler is smart enough, it will detect that the 0 can be optimized.

What is important to take care is the order of the comparisons when you have more than one:

if(conditionA && conditionB && (conditionC || conditionD)) {...}

In this case, you should put the conditionA as the one that will cause the if to fail faster, instead of letting the execution analyze all other conditions and only then see that conditionA fails.


Try writing small programs such as the one below, and you can determine which suits your needs. Are you looking for small code size, or fast execution?

#include <stdio.h>
#include <stdlib.h>

    int main(int argc, char *argv[]) {
        int x = atoi(argv[1]);
        int zeros=0;
        int i;
        for (i=0; i<100000; i++) {
            if (0==x) {
                zeros++;
            }
        }
        printf("zeros: %d\n",zeros);
        return 0;
    }


Why not try to benchmak the 2 solutions ? :

#include <stdio.h>
#include <time.h>

#define LOOPS   1000000000

void main(void)
{
    clock_t start1, start2, end1, end2;
    int x=1,i;

    start1=clock();
    for(i=0;i<LOOPS;i++)
    {
        if (x==0)
        {
            x=1;
        }
    }
    end1=clock();

    start2=clock();
    for(i=0;i<LOOPS;i++)
    {
        if (0==x)
        {
            x=1;
        }
    }
    end2=clock();

    printf("x==0 %d ns\n", end1-start1);
    printf("0==x %d ns\n", end2-start2);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消