性能:大于/小于 vs 不等于 [英] Performance: greater / smaller than vs not equal to

查看:25
本文介绍了性能:大于/小于 vs 不等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道两者在性能上是否存在差异

I wonder if there is a difference in performance between

检查一个值是否大于/小于另一个值

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

检查一个值是否不等于另一个

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

为什么?

另外:如果我比较为零,是否还有进一步的区别?

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.

正如你们大多数人指出的那样,性能差异当然可以忽略不计,但我对 cpu 级别的差异感兴趣.哪个操作更复杂?

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.

我删除了 Java 标记,这是我不小心添加的,因为这个问题通常不仅仅基于 Java,抱歉.

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

推荐答案

你还是应该做更清晰、更安全、更容易理解的事情.这些微调讨论通常是在浪费您的时间,因为

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

  • 它们很少能产生可衡量的差异
  • 当它们发挥作用时,如果您使用不同的 JVM 或处理器,这种情况可能会发生变化.即没有警告.

注意:生成的机器也会随着处理器或 JVM 的变化而变化,因此在大多数情况下,即使您非常熟悉汇编代码,查看这也不是很有帮助.

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.

这篇关于性能:大于/小于 vs 不等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆