case 语句或 if 语句效率视角
可能的重复:
“else if”比“switch() case”更快吗?
相对性能差异是多少Java 中 if/else 与 switch 语句的区别?
我知道 case 语句可以通过跳转表来实现。这是否使它们比 if 语句更有效?
这只是应该避免的微观优化吗?
Possible Duplicates:
Is "else if" faster than "switch() case"?
What is the relative performance difference of if/else versus switch statement in Java?
I know that case statements can be implemented with jump tables. Does this make them more efficient than if statements?
Is this just micro-optimization that should be avoided?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
大概是没关系吧。字节码只是 JVM 的一种“传输格式”。 JVM 内部发生的情况与字节码表示非常不同。 (例如:字节码不提供浮点运算,因此 float +-*/% float 作为 double 运算完成,然后将结果转换回 float。对于 byte/short 也是如此,它们会转换为 int,然后再转换回来.) 但对于 switch 来说,它们是两种字节码格式,一种已经带有跳转表。但老实说:我会选择最适合您和程序读者的格式。 JVM 将完成剩下的工作。如果你太聪明,你的 JVM 可能不会明白你的意思,最终程序会变慢。
“我们应该忘记小效率,大约 97% 的情况下:过早优化是万恶之源”D. Knuth
Probably is doen't matter. Bytecode is just a "transport format" to the JVM. What happens insite the JVM is very different from the bytecode representation. (Example: Bytecode doesn't offer float operations, so float +-*/% float is done as double operations and then the result is casted back to float. Same is true for byte/short, they are converted to int and then back.) But for switch they are two bytecode formats, one already with a jump table. But honestly: I would choose a format which is best for you and the reader of your program. The JVM will do the rest. If you are too smart you JVM maybe don't get your point and in the end the program is slower.
"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil" D. Knuth
如果您有一个非常大的 if else 语句链,那么,是的,您可能会感觉到差异。但编写这么长的 ifelse 链是非常不现实的。即使您这样做了,这仍然不太可能是您的性能瓶颈所在。
首先编写可读的代码,然后在需要进行性能优化时让分析器为您提供指导。
If you had a very large chain of if else statements, then, yes, you might feel the difference. But it is very unrealistic that you'll ever write such a long ifelse chain. And if even you did, it's still very unlikely that that is where your performance bottleneck would be.
Write your code to be readable first, and let yourself be guided by a profiler when the need for performance optimization arises.
如果任何编译器可以验证这些值是否相当紧凑,它都会创建跳转表。 (我怀疑在这种情况下它们是否是 10 的倍数。)
这是一个微观优化。仅当您知道微优化有意义时,它才有意义。通常,其他地方还有更大的“鱼要炸”,以函数调用的形式可以不使用。但是,如果您已经调整了这段代码的日光,并且您的分析显示很大一部分(例如 10% 或更多)时间用于这些 IF 语句(而不是其内容),那么它会有所帮助。例如,这可能发生在字节码解释器中。
补充:我喜欢使用
switch
的另一个原因是,即使它不创建跳转表 - 当在调试器中单步执行代码时,它会直接转到正确的情况,而不是让我单步执行大量错误的if
语句。使调试更加容易。Any compiler will make the jump table if it can verify that the values are reasonably compact. (I doubt if they are in this case, being multiples of 10.)
This is a micro-optimization. Micro-optimization makes sense only if you know that it does. Typically, there are larger "fish to fry" elsewhere, in the form of function calls that could be done without. However, if you have already tuned the daylights out of this code, and your profiling shows that a good fraction (like 10% or more) of time is going into these IF statements (and not to their contents) then it helps. This can happen, for example, in a byte-code interpreter.
Added: Another reason I like to use
switch
is, even if it doesn't make a jump table - when stepping through the code in a debugger, it goes directly to the proper case, rather than making me step through a lot of falseif
statements. Makes it easier to debug.我认为最主要的是把代码写得尽可能清晰。像这样的微观优化不应该成为焦点。
例如,如果您有这样的事情:
那么使用 switch 语句会更清楚:
同样,我会专注于使代码更易于阅读并保持纳秒级的效率提升。
I think the main thing is to write the code as clearly as possible. Micro-optimizations like this shouldn't be the focus.
For example, if you have something like this:
Then it's clearer to use a switch statement:
Again, I would focus on making the code easiest to read and maintain rather than nano-second level efficiency gains.