-1

What's the relative efficiency of integer comparison in java?

== >= <= > <

Does it depend on the physical architecture?

user1277170
  • 3,127
  • 3
  • 19
  • 19
  • 9
    Please, for the love of whatever gods you may or may not believe in, don't micro-optimise. There are almost always far better ways to wring performance out of your code. – paxdiablo Jun 12 '12 at 05:30
  • 4
    There are almost certainly many other things causing larger slowdowns in your code than int comparisons. – Andrew Marshall Jun 12 '12 at 05:31
  • 1
    @paxdiablo - I think the God you are referring to might be Knuth the God of Algorithms. You don't want to MIX with him :-) – Stephen C Jun 12 '12 at 05:46
  • 1
    So what can you go to ensure the answers are improved. ;) Its hard to give a good answer to a poor question or if the questioner doesn't follow up on a misunderstanding. – Peter Lawrey Jun 12 '12 at 06:34

4 Answers4

4

I doubt there's a lot of difference. If there is, chances are good that the compiler will transform your expression into the best test. There should be no difference at all if one of the arguments is 0.

If you're doing something where this makes any kind of difference at all, I'd like to know what it is.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    +1 for the last sentence. I'd challenge the OP to provide a credible benchmark that proves it makes a difference. – Stephen C Jun 12 '12 at 05:42
  • Object orientated efficiency? LOL. Of course anything saved will be overwhelmed with the paradigm. I just couldn't remember, but could remember that there could be differences. – user1277170 Jun 12 '12 at 05:44
0

Yes, it depends on both your hardware architecture and your JVM: Java runs within a virtual machine which implements the Java Virtual Machine Specification see http://docs.oracle.com/javase/specs/jvms/se7/html/index.html . There are a wide variety of JVMs which all implement java code differently (e.g. those on this list: https://en.wikipedia.org/wiki/List_of_Java_virtual_machines ) Each JVM and processor architecture can implement your code differently, so your exact results will vary according to them.

That said, the other answers are correct that these operators should all be effectively identical in practice with real code. Other operations (such as multiplication, floating point operations and more complex calculation) will have a vastly greater impact on processor time because they are not as simple to carry out.

kaz
  • 675
  • 2
  • 5
  • 13
0

They are all single opcodes of the form if_acmp<cond>, if_icmp<cond>, if<cond>, etc. in the Java bytecode, and I can't immediately recall any hardware architecture where there were significantly different opcodes either for the various conditions, or different cycle times. In one or two interpreters I have built I saved code space by only implementing <, ==, and >, and doing the others with a following NOT, but that was just for toy languages.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Most hardware instruction sets do these comparisons as two instructions a compare followd by a branch depending on the result of the compare. So for most JVMs on most hardware it makes no difference at all whether its <, <=,= >=, > or !=, its the same two instructions with different flags set.

See this question

Of course it entirely depends on the instruction set and how the JVM authors and their C compiler chooses to use it.

Community
  • 1
  • 1
James Anderson
  • 27,109
  • 7
  • 50
  • 78