java 使用逻辑运算符代替 if-else if 进行返回
我在 equals 方法中有以下代码。
public boolean equals(Object o){
if (o == null) return false;
if (o == this) return true;
if (!(o instanceof Vertex)) return false;
return ((Vertex) o).label().equals(label);
}
我的 IDE 突出显示了 if 语句,并希望我基本上执行此操作。
public boolean equals(Object o){
return (o != null) && ((o==this) || ((o instanceof Vertex) && ((Vertex) o).label().equals(label);
}
有人告诉我,编译器通常足够智能,可以进行优化,并且通常应该为可读性编写代码。因此,显然第二个代码示例不像第一个代码示例那么容易阅读。我的 IDE 是否只是令人烦恼,还是这样做有一些实际的性能优点?
I have the following code in an equals method.
public boolean equals(Object o){
if (o == null) return false;
if (o == this) return true;
if (!(o instanceof Vertex)) return false;
return ((Vertex) o).label().equals(label);
}
My IDE highlights the if statement and wants me to basically do this
public boolean equals(Object o){
return (o != null) && ((o==this) || ((o instanceof Vertex) && ((Vertex) o).label().equals(label);
}
I've been told that the compiler is generally smart enough to optimize and that in general one should code for readability. So, clearly the second code sample is not as easy to read as the first. Is my IDE just being annoying or is there some actual performance merit to doing it that way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,只有在您知道这是瓶颈时才进行优化,否则要提高代码的可读性。
您可以检查字节码来查看,但我怀疑它们即使不完全相同,也非常接近。即使字节码存在细微差异,我也看到 JIT 编译器将内容优化到没有差异的程度。您始终可以进行性能测试来确定。
First, only optimize if you know it's a bottleneck, else code for readability.
You can check the byte code to see but I suspect they are pretty close if not exactly the same. Even if there are slight differences in bytecode, I've seen the JIT compiler optimize stuff down to where there is no difference. You can always do performance tests to be sure.
大多数 IDE 都是可配置的,允许您指定其输出的警告样式类型。您是对的,按照编译器的建议执行生成的代码没有任何好处。编译器确实应该能够优化这些事情。
Most IDEs are configurable and allow you to specify the kinds of style warnings it outputs. You are correct that there is no benefit to doing what the compiler suggests in terms of the code that is generated. Compilers should indeed be able to optimize these kinds of things.
您的 IDE 建议的版本几乎没有任何性能优势。此外,你说得对,JVM(不仅仅是编译器)可以做很多优化。
正如其他人指出的那样,尽量提高可读性。你编写的代码被人类读取,并由Java编译器和虚拟机编译、修改和优化。
顺便提一句。一个简短的提示:您的代码可以像这样进行优化(省略等于
null
的条件,因为o
在第二个之后保证是 Vertex 的实例健康)状况):The performance benefit of the version suggested by your IDE is virtually none. Furthermore, you are right that JVM (not only compiler) can do a lot of optimizations.
As others pointed out, try to reach for readability. The code you write is read by human beings and compiled, modified and optimized by Java compiler and virtual machine.
Btw. a short hint: your code can be optimized a little more like this (leaving out the condition for equality to
null
, sinceo
is guaranteed to be instance of Vertex after the second condition):不要试图让编译器变得聪明。您正在编写供人阅读的代码。让编译器担心将其转换为机器可读的格式。
Don't try to out smart the compiler. You are writing code for a person to read. Let the compiler worry about converting it to machine readable format.
这样做的好处是,如果您内联代码,那么您可以轻松地将其分配给布尔值,但您也可以使用三元运算符使其更具可读性,这将比优化版本更具可读性,但会具有相同的功能,如果您了解如何做到这一点,可能会帮助您稍后简化编码。
这应该仍然可读。
我还没有尝试编译这个,可能需要一些额外的括号,但我要寻找一个概念,而不是你可以复制和复制的东西。粘贴。
The benefit to doing it this way is that if you inline the code then you can assign it easily to a boolean, but you could also use a ternary operator to make it more readable, which will be more readable than the optimized version, but would have the same functionality, and may help you to simplify coding later if you learn how to do it.
This should still be readable.
I haven't tried to compile this, some extra parenthesis may be needed, but I am going for a concept not for something you can copy & paste.
为什么不自己测试一下性能呢?这是一个示例:
将为您计时 run 方法。所以你可以这样称呼它:
Why not test the performance your self? Here is a sample:
will time the run method for you. So you can call it like this: