J
junky_fellow
which of the following is faster and why ?
if ( x == 1 ) {
}
or
if ( x != 0 ) {
}
if ( x == 1 ) {
}
or
if ( x != 0 ) {
}
[email protected] said:which of the following is faster and why ?
if ( x == 1 ) {
}
or
if ( x != 0 ) {
}
which of the following is faster and why ?
if ( x == 1 ) {
}
or
if ( x != 0 ) {
}
which of the following is faster and why ?
if ( x == 1 ) {
}
or
if ( x != 0 ) {
}
August Karlstrom said:In practice it would probably be
if (x) { ... }
Keith Thompson said:On the other hand, comparing the speed of "x != 0" vs. "x == 1" is
unlikely to be useful, since they aren't semantically equivalent
(unless you can prove that the value of x is either 0 or 1).
The two comparisons are different.which of the following is faster and why ?
if ( x == 1 ) {
}
or
if ( x != 0 ) {
}
Keith said:.... snip ...
Not likely. "if (x)" is precisely equivalent to "if (x != 0 )".
There's no reason to expect that either will be faster than the
other. For any reasonable compiler, both will result in exactly
the same generated code.
which of the following is faster and why ?
if ( x == 1 ) {
}
or
if ( x != 0 ) {
}
The rule of thumb for speed is:
1. If it doesn't work then speed doesn't matter.
Christian said:A pointless question, since they don't do the same thing.
As to which is faster, neither. They break the
sequential flow of instructions. This break may
take up more time than the execution of the
instructions.
I have never ever had to bother with the speed of integer tests, except
maybe on some microcontrollers and way back on Z80 processors (4 MHz,
this does blow your mind doesn't it?)
And even in those cases, I would
write routines in assembly language if speed was really a concern.
That said, even the dumbest C compiler will compile this kind of test
in the optimal way. You'd need to get a very, very old compiler to
actually find a difference in the object code.
The same goes for integer multiply/divide by a power of 2. Except when
it is clearer to the reader, you don't need to use shifting operators.
Over 12 years ago, Think C on the Mac already knew how to optimize
n / 2 as a right shift operation.
So, as other have said, you're most likely to be wasting your time
with such questions...
_For unsigned integers_. For signed, the round-in required in C99 andThe same goes for integer multiply/divide by a power of 2. Except when
it is clearer to the reader, you don't need to use shifting operators.
Over 12 years ago, Think C on the Mac already knew how to optimize
n / 2 as a right shift operation.
_For unsigned integers_. For signed, the round-in required in C99 and
universally or nearly so implemented though not required in C89,
differs from and thus can't be optimized to right shift on two's
complement which also is nearly universal but not actually required.
Dave Thompson said:_For unsigned integers_. For signed, the round-in required in C99 and
universally or nearly so implemented though not required in C89,
differs from and thus can't be optimized to right shift on two's
complement which also is nearly universal but not actually required.
tigervamp said:It depends on your platform, the Standard has nothing to say about
this. Why don't you test it yourself on your machine?
You should also note that the above tests are not equivalent. If x is
2 then the first test will be false while the second will be true. You
should be more worried about writing clear code that makes sense in
your situation and let the compiler worry about how to best perform the
test for you since it undoubtedly knows more than you do about how best
to optimize such an expression.
Rob Gamble
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.