0

Since both !< and >= have the same function I'm wondering if one is computed faster in Java, and other languages. I don't know how you would test this so that's one reason why I'm asking. I've heard that Java processes 32bit nums (int) faster than 8 bit nums (byte) since it's based on 32 bit infrastructure. Due to the aforementioned sentence I'm wondering if !< or >= is computed faster in Java (or if neither of them are).

*** Edit: Bytes are 8 bits, not 4 bits.

  • I suggest adding a simple code example for clarity. (And thanks for asking this as I too have wondered about this.) – Basil Bourque Dec 15 '21 at 07:25
  • 1
    Relevant: [How do I write a correct micro-benchmark in Java?](https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java) – Bohemian Dec 15 '21 at 07:25
  • 5
    There is no `!<` operator in java. So you mean to first do `<` and then negate that? – GhostCat Dec 15 '21 at 07:26
  • 5
    And note: why exactly are you asking? (a lot of people who have such kind of questions assume that they need to *worry* about the answer in every day programming, and well: you absolutely shouldn't worry about such things in every day programming). So unless you now say: "pure theoretical interest", the answer is: don't worry about it. And the actual answer would: that probably depends what the underlying CPU does. Because if the code under test "matters", the JIT will turn it into machine code, and then it will use whatever optimal machine instruction is available. – GhostCat Dec 15 '21 at 07:27
  • 6
    This is not an efficient use of brain cycles. Focus on making your code readable and functional. – shmosel Dec 15 '21 at 07:37
  • *"I've heard that Java processes 32bit nums (int) faster than 4 bit (byte) since it's based on 32 bit infrastructure."* Java does not have a 4-bit numeric type, 4 bits is not a byte, and this has nothing to do with the performance of `<` vs. `>=`. – kaya3 Dec 15 '21 at 07:59
  • Note that your edit didn't address *any* of the issues addressed in comments to you. Please understand: you expect other people to spend their time to help you for free. So YOU please take the time to listen to their feedback and improve your requests accordingly. – GhostCat Dec 15 '21 at 08:12

1 Answers1

3

No, it does not make a difference. The generated Java Byte Code for both variants the pretty much the same in both cases.

You can use godbolt for comparison: Link

The only difference is the use of dcmpg instead of dcmpl for double values, which should not affect the speed:

https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html

The dcmpg and dcmpl instructions differ only in their treatment of a comparison involving NaN. NaN is unordered, so any double comparison fails if either or both of its operands are NaN. With both dcmpg and dcmpl available, any double comparison may be compiled to push the same result onto the operand stack whether the comparison fails on non-NaN values or fails because it encountered a NaN. For more information, see §3.5.

EDIT: As pointed out by @GhostCat a JIT compiler may generate different machine code based on the underlying hardware.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Simon Kraemer
  • 5,700
  • 1
  • 19
  • 49