Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jvm - Performance: greater / smaller than vs not equal to

I wonder if there is a difference in performance between

checking if a value is greater / smaller than another

for(int x = 0; x < y; x++); // for y > x

and

checking if a value is not equal to another

for(int x = 0; x != y; x++); // for y > x

and why?

In addition: What if I compare to zero, is there a further difference?

It would be nice if the answers also consider an assebled view on the code.

EDIT: As most of you pointed out the difference in performance of course is negligible but I'm interested in the difference on the cpu level. Which operation is more complex?

To me it's more a question to learn / understand the technique.

I removed the Java tag, which I added accidentally because the question was meant generally not just based on Java, sorry.

question from:https://stackoverflow.com/questions/18596300/performance-greater-smaller-than-vs-not-equal-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should still do what is clearer, safer and easier to understand. These micro-tuning discussions are usually a waste of your time because

  • they rarely make a measurable difference
  • when they make a difference this can change if you use a different JVM, or processor. i.e. without warning.

Note: the machine generated can also change with processor or JVM, so looking this is not very helpful in most cases, even if you are very familiar with assembly code.

What is much, much more important is the maintainability of the software.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...