6

Consider the following scenario:

  • I have 2 constants MAX & MIN

  • I get a new number x

Now to check if x in the given rage I would do something like this:

if(x >= MIN && X <= MAX)
{
   //Some logic
}

I wondering if there's a better way to go talking about efficiency. I do know that's a very simple task, but I am just curious to know if there's a better way

Sid
  • 14,176
  • 7
  • 40
  • 48
  • 8
    Why do you think there is something more efficient than simply comparing with `>=` and `<=`? Why do you care at all? – Tim Schmelter Nov 14 '16 at 11:31
  • 1
    What can be more efficient than two comparisons and a logical "and"? These are only three atomic processor OPs. In some cases there will be only one OP (logical short-circuit). – dymanoid Nov 14 '16 at 11:31
  • Comparison operations are among the cheapest of the cheap - the CPU implements them in hardware and they really only consume one instruction (plus memory load). There is no way that this could possibly be a performance bottleneck so it really doesn't bear more scrutiny than that. Keep it simple and don't think too hard about problems that aren't problems. – J... Nov 14 '16 at 11:32
  • Are you facing performance issues with that code? – Fabio Nov 14 '16 at 11:33
  • More convinent, perhaps. more efficient, I don't think there is. – Zohar Peled Nov 14 '16 at 11:33
  • @TimSchmelter I am not sure if there's anything more efficent than the snippet I provided, I was just wondering if there's anything that I am missigin about this simple snippet as it is very common – Sid Nov 14 '16 at 11:34
  • 1
    @dymanoid the more efficient way is a single subtraction along with a no-op cast and a comparison. See the duplicate. However that's most probably optimized automatically by modern compilers – phuclv Nov 14 '16 at 16:15
  • @TimSchmelter there is indeed a more efficient way that's most modern C/C++ compilers are able to optimize for this case, although I don't know if C# compilers and JITers are smart enough to do this or not – phuclv Nov 14 '16 at 16:17

2 Answers2

12

First of all this is a typical case of micro-optimization that almost never pays off.

Having said that, the only way to 'optimize' this is if you know in advance that one of the comparisons is highly likely to produce false. If there is one, then put that comparison first, to take advantage of boolean short-circuit evaluation.

if (x >= MIN && x <= MAX) { ... } // most efficient if x >= MIN is hardly ever true

or

if (x <= MAX && x >= MIN) { ... } // most efficient if x <= MAX is hardly ever true

And if neither comparison is predictable, then we have all wasted time...

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • Shouldn't your comments be opposite, like: `if (x >= MIN && x <= MAX) { ... } // most efficient if x >= MIN is mostly true` So, that it gets through on the first comparison itself and needn't have to evaluate logical And operator – Mrinal Kamboj Nov 14 '16 at 11:38
  • No, you are reasoning exactly opposite. False leads to short-circuiting for `&&`: `false && whatever == false`. – Peter B Nov 14 '16 at 11:44
  • You are pointing to the efficiency, when number is not in a range, by virtue of first boolean comparison as false, but that may not in reality meet the OP's requirement, who might want to process logic for a value in range. So yes theoretically you are correct, I was wrong earlier but in reality both the above implementation would have efficiency, not much is different. – Mrinal Kamboj Nov 14 '16 at 11:57
  • this is not the only way to optimize it, please see the duplicate. Moreover you can use [`__builtin_expect`](http://stackoverflow.com/q/30130930/995714) to indicate which branch to take instead of manually organizing the comparisons like that – phuclv Nov 14 '16 at 16:11
4

If you know that your min value will be 0 (you want to exclude all negative numbers), you can reduce the if statement to

if (max <= (uint)x)
CookedCthulhu
  • 744
  • 5
  • 14