case 语句或 if 语句效率视角

发布于 2024-09-12 03:00:58 字数 448 浏览 6 评论 0原文

可能的重复:
“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

≈。彩虹 2024-09-19 03:01:04

大概是没关系吧。字节码只是 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

以歌曲疗慰 2024-09-19 03:01:04
  1. 否,这是程序设计的一部分。但是您应该考虑对于一系列类型,可重写的方法是否可能不是更好的解决方案。
  1. Yes
  2. No, it is part of your program design. But you should consider whether an over-ridable method mightn't be an even better solution, with a family of types.
淡淡離愁欲言轉身 2024-09-19 03:01:03

如果您有一个非常大的 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.

谁人与我共长歌 2024-09-19 03:01:02

如果任何编译器可以验证这些值是否相当紧凑,它都会创建跳转表。 (我怀疑在这种情况下它们是否是 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 false if statements. Makes it easier to debug.

谜泪 2024-09-19 03:01:01

我认为最主要的是把代码写得尽可能清晰。像这样的微观优化不应该成为焦点。

例如,如果您有这样的事情:

if (age == 10) {
  // ...   
} else if (age == 20) {
  // ...   
} else if (age == 30) {
  // ...   
} else if (age == 40) {
  // ...   
}

那么使用 switch 语句会更清楚:

switch (age) {
    case 10:
        // ...
        break;
    case 20:
        // ...
        break;
    case 30:
        // ...
        break;
    case 40:
        // ...
        break;
}

同样,我会专注于使代码更易于阅读并保持纳秒级的效率提升。

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:

if (age == 10) {
  // ...   
} else if (age == 20) {
  // ...   
} else if (age == 30) {
  // ...   
} else if (age == 40) {
  // ...   
}

Then it's clearer to use a switch statement:

switch (age) {
    case 10:
        // ...
        break;
    case 20:
        // ...
        break;
    case 30:
        // ...
        break;
    case 40:
        // ...
        break;
}

Again, I would focus on making the code easiest to read and maintain rather than nano-second level efficiency gains.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文