简体   繁体   中英

Is it better to use x >= 0 or x > -1

I haven't been able to find this seemingly simple question answered anywhere. Is it better to compare an integer to 0 by using x >= 0 or x > -1?

I'm sure it doesn't make a noticeable difference either way, however it would be interesting to understand the inner workings better. I'm specifically talking about Java, but if other languages are different, it would be interesting to know. When I say better, I mean in terms of efficiency, as obviously, x >= 0 is easier to read.

It actually doesn't matter in this case. The difference between x >= 0 and x > -1 is irrelevant when dealing with integers.

Now, that isn't to say there is no difference at all. >= checks for "greater than or equal to", whereas > checks only for "greater than". Now, if you were dealing with floating-point numbers as well as integers here, then you would definitely want to use x >= 0 , as x > -1 would evaluate to true for any negative x value where -1 < x < 0 , as well as for 0 and positive numbers.

Now, I would personally recommend that, in any case where you have to choose between >= and > (or <= and < ), you choose the one that would be correct when working with floating-point numbers. Even though it technically doesn't matter when dealing strictly with integers, it is good practice to always consider the difference between the two when using them, even in cases where using either would result in the same outcome.

You haven't been able to find an answer because the question can't be answered.

I've seen code like if (i > 50) i=50 compile to something like this in bytecode instructions:

Address         Hex             Opcode              Operand
....
0000002D        3B              istore_0
0000002E        10 32           bipush              50
00000030        A4 00 06        if_icmple           pos.00000036
00000033        10 32           bipush              50
00000035        3B              istore_0
00000036        ....

if_icmple : if value1 is less than or equal to value2, branch to instruction at branchoffset

So apparently, the compiler translated the if (i > 50) i=50 to something like: if (i <=50) , skip the assignment i=50 . If it's more efficient, I don't know, but it does show that changing expressions like you suggest is useless because the compiler may change expressions or invert if statements at compile time.

Also, my CPU doesn't understand java bytecode, but the java bytecode will be translated to machinecode by a jit compiler. And guess what... jit compilers can do optimization as well. So even looking at the java bytecode won't be conclusive. And since you don't know what CPU or jvm I'll be running your program on, you simply can't know!

(And even if you could know the answer, sacrificing readability for a micro optimization like that is not a good idea. The benefit would be very little, at a great cost!)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM