1

I have a colleague in work who is arguing with me that negated conditions are faster when used in if statements. I have argued back that the compiler is optimizing the code and we don't really know how this condition will actually end.

For example, she is claiming that

if(!MyCondition)

is faster than

if(MyCondition)

I have searched and could not find a reference for this anywhere. So, my question is:

Is there really any preference to improve performance when using conditions inside if statements? Or it's just a matter of better readability?

Dola
  • 1,483
  • 11
  • 16
  • 1
    It's nonsense. It's always going to introduce an extra IL instruction. – Matthew Watson Sep 25 '15 at 07:54
  • 2
    Ask her what her reasoning is. – Blorgbeard Sep 25 '15 at 07:54
  • 1
    My response would be: prove it. Show a significant performance difference. I would expect no difference with the output of the JITer using the x86/x64 instructions for branch if zero or branch if not zero which should have the same overhead: completely dominated by pipeline flushing. – Richard Sep 25 '15 at 07:55
  • When someone makes such a claim, they're the one that need to come up with sources. – CodeCaster Sep 25 '15 at 07:55
  • Either way, one of them produces a bug since it changes what enters the if statement. [Race the horses](http://ericlippert.com/2012/12/17/performance-rant/) – Sayse Sep 25 '15 at 07:56
  • 4
    I'm voting to close this question as off-topic because no evidence is provided that there is any detectable difference. – Richard Sep 25 '15 at 07:56
  • 1
    Most of these statements are irrelevant anyway. You're right to enquire, especially if you're genuinely interested - but the performance implications (absolutely minuscule, I'd imagine) vs. the change in development style just isn't a contest for me. So many questions on SO are made with regard to optimizing performance much earlier than necessary. You might ask 'well what harm can it do?' - you lose time on it. When you're learning, that could be time spent a lot more productively. And losing time like this in a business context - well, ask your manager about that. –  Sep 25 '15 at 08:00
  • I have just carried out a sample for 100000 iterations & found out that she is Right !!! theres indeed a difference of 230 ms.. :P – Nikita Shrivastava Sep 25 '15 at 08:07
  • @Nikita then you'd better ask a new question and show it to us – Fabjan Sep 25 '15 at 08:10
  • I am not sure why you see this question off-topic, I believe it's a legitimate question regarding performance. – Dola Sep 25 '15 at 08:14
  • 2
    @Nikita 100,000 iterations don't take 230 ms, let alone show a difference that big. Write a proper benchmark. – CodeCaster Sep 25 '15 at 08:14
  • Just an inserting thing. but this is just about mathematics not in programming languages and algorithms .In mathematics some functions are slow to give you the result. like the square root. we have [inverse functions](https://en.wikipedia.org/wiki/Inverse_function) which are extremely faster than normal functions. inverse square root is extreme fast. the same thing about logarithm.sometimes in mathematics inverse functions are faster than functions. – M.kazem Akhgary Sep 25 '15 at 08:14
  • 3
    @Dola the problem with questions like this, especially when stated as such, is that they aren't interesting nor factual. _"My colleage says X is faster than Y"_ is very nice for that colleague, but not for the rest of the internet. Click the ["race the horses"](http://ericlippert.com/2012/12/17/performance-rant/) link, write a proper benchmark and then if you see a significant difference, ask why that is. – CodeCaster Sep 25 '15 at 08:16
  • 2
    This question has been discussed at http://stackoverflow.com/questions/1029992/is-the-inequality-operator-faster-than-the-equality-operator – Viet Nguyen Sep 25 '15 at 08:17
  • @CodeCaster I stayed incorrect & thanks to you to point that out, the difference is 00.000230 ms, absolutely negligible as it seems but proves his collegue right ! – Nikita Shrivastava Sep 25 '15 at 08:20
  • @Nikita you can't make measurements that small accurately on your hardware. Again, do some research and write a proper benchmark. :) – CodeCaster Sep 25 '15 at 08:21
  • @CodeCaster for me the difference is `0.1` seconds for 1 billion comparisons. – M.kazem Akhgary Sep 25 '15 at 08:23
  • @M.kazemAkhgary I don't see your benchmarking code, so I can safely discard your statement. Again, proper benchmarking is _hard_. – CodeCaster Sep 25 '15 at 08:26
  • @CodeCaster I did what you have suggested, my only problem is how should i post it?(I am a lil new to SO land,just a month old ) :P – Nikita Shrivastava Sep 25 '15 at 08:27
  • Well for 4000 times and 1M comparisons the result was that they practically has same performance (3,03925 ms for == operator vs 3,01075 ms for != operator 4 000 000 000 logical operations (Debug mode)) but in release mode things changed and != is faster – Fabjan Sep 25 '15 at 08:35

1 Answers1

9

Premature Optimization is the Root of all Evil

Always go for readability.

if ( hasRightXYZ )

vs

if ( ! hasRightXYZ )

does not make such a big deal in terms of readability. But if the condition gets complex always hunt for greater readbility.

if ( hasRightXYZ || hasRightYZX || hasRightZYX )

vs

if ( ! hasRightXYZ && ! hasRightYZX && ! hasRightZYX )

If you are looking for performance improvements a simple negation is not the thing you want to hunt down. Rather typical N+1 Select scenarios or some other stuff.

Stephan Schinkel
  • 5,270
  • 1
  • 25
  • 42