Which one is faster?

Started by
20 comments, last by akiinao 21 years, 10 months ago
Which one is faster: comparison of equality or non-equality?
Advertisement
For integers, it''s the same. It''s just a je/jne difference once compiled. I don''t know the FPU instructions but I guess it''s the same for floats.
I would say the comparison of non-equality is faster.

int x = 10;
int y = 1233;

if (x != y) {}

if the comparison is done bitwise and you are checking for equality you have to compare every bit to be shure that the values are the same. If you check for non-equality you can stop comparing as soon as one bit isn't equal to the other. But if you compare two equal values of non-equality it needs the same amount of time, i think...

[edited by - Kaesebrot on May 28, 2002 12:33:43 PM]
Prosper/LOADED is correct. In both cases a CMP instruction is run on the two values, which sets the proper flags (including the equal, greater, less than, and zero flags), after that you just use a conditional jump statement (JE to jump if the equal flag is set, JNE to jump if it is not set). The only comparisons that are faster are when you are comparing a value to zero (and it is still the same whether you do equal or not equal to zero).
Processors usually have explicit instructions for comparing with zero, so in theory doing something like this:

if (result != FALSE)
{
}

could be quicker, or maybe just smaller in code, than

if (result == TRUE)
{
}

Whether that''s actually true though is a different matter.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
They''re the same: generaly the assembler to test for equality and inequality will be identical except the result of the actual check will be inverted, for which a identically performming different/variant assember instruction will be provided.

But on modern processors there can be quite a difference in performance due to branch prediction. This is a big topic which has a big impact on code performance, and is influenced by such things as processor design, compiler design and to some extent on how you write your code. It''s worth looking up branch prediction if the performance of such tests is very imporant to you.
John BlackburneProgrammer, The Pitbull Syndicate
quote:Original post by siaspete
Processors usually have explicit instructions for comparing with zero, so in theory doing something like this:

if (result != FALSE)
{
}

could be quicker, or maybe just smaller in code, than

if (result == TRUE)
{
}


Yes, compare with zero is faster on some processors: e.g. the MIPS has a zero register available for such quick comparison, while doing

if (result == TRUE)
{
}

asks the compiler whether result is equal to 1, the value of which needs to be loaded into memory.

Better to do:

if (result)
{
}

which is equivalent to

if (result != FALSE)
{
}

and is generally faster
John BlackburneProgrammer, The Pitbull Syndicate
but many optiming compilers will turn
if(result==TRUE) to if(result!=FALSE)
.
quote:Original post by a person
but many optiming compilers will turn
if(result==TRUE) to if(result!=FALSE)


Yes, but they will also turn
result!=TRUE into result==FALSE

Zero is a special case.

This topic is closed to new replies.

Advertisement