PDA

View Full Version : Qt true,false speed



mero
9th October 2010, 18:00
Hello,

Is there any difference in speed in writing code like Code1 and Code2 ?
I heard that Code2 is faster than Code1... ?

Code1:


if (strData.isEmpty() == true)
{
...


Code2:


if (strData.isEmpty())
{
...

SixDegrees
9th October 2010, 18:10
You would have to check a disassembly to be absolutely sure, but the comparison has to be made in either case, and most compilers will produce equivalent code for both.

Any difference would be so small it would be difficult to measure. The effect on runtime would be negligible.

squidge
9th October 2010, 20:59
Well, the second code is certainly faster than the first to type, maybe thats what they were talking about?

As for speed, well, you have two different comparisons, so it depends on how its compiled. The first checks against true only, whilst the second checks for any non-false value, so the second might be one assembly instruction less on some architectures (as they can just check a flag rather than comparing with a fixed value), but as already said, the difference would be incredibly small.

I'd go with the second simply because it's quicker to type :)

SixDegrees
9th October 2010, 21:09
I'd probably go with the second as well, but only because the function call (isEmpty()) makes it very clear what is being tested. In other cases, the function name alone is often not so clear, and adding the explicit test condition removes all doubt as to what the expected outcome is. From a maintenance standpoint, clarity often trumps small differences in performance.