Java 将使用一个";否则";加快代码的执行速度?
如果我的一些方法中有一些代码最多包含9个Java 将使用一个";否则";加快代码的执行速度?,java,if-statement,Java,If Statement,如果我的一些方法中有一些代码最多包含9个If语句,并且所有这些语句本质上只满足If要求中的一个,那么使用else If会加快我的代码速度吗 例如,如果我有这个 if (x == 1){ do something; } if (x == 2){ do something; } if (x == 3){ do something; } if (x == 4){ do something; } 将其更改为此会加快代码的速度吗 if (x == 1){ do s
If
语句,并且所有这些语句本质上只满足If
要求中的一个,那么使用else If
会加快我的代码速度吗
例如,如果我有这个
if (x == 1){
do something;
}
if (x == 2){
do something;
}
if (x == 3){
do something;
}
if (x == 4){
do something;
}
将其更改为此会加快代码的速度吗
if (x == 1){
do something;
}
else if (x == 2){
do something;
}
else if (x == 3){
do something;
}
else if (x == 4){
do something;
}
在我的实际代码中,
if
语句计算的是比整数更复杂的东西。可能。这取决于编译器是否能够检测到您的条件是互斥的,并在此基础上进行优化。在简单的整数情况下,它可能可以;在更复杂的情况下,它不能
但这不仅仅是编译器的问题。这也是关于读者的。使用else if
向读者说明案例是相互排斥的,这是一件好事
另一个选项是在每个if块的末尾从函数返回。可能。这取决于编译器是否能够检测到您的条件是互斥的,并在此基础上进行优化。在简单的整数情况下,它可能可以;在更复杂的情况下,它不能 但这不仅仅是编译器的问题。这也是关于读者的。使用
else if
向读者说明案例是相互排斥的,这是一件好事
另一个选项是在每个if块的末尾从函数返回。
else
仅在if
不执行时执行,因此肯定会加速。但是else
意味着在没有发生的情况下执行此操作。else
仅在没有发生的情况下执行,因此它肯定会加速。但是else
意味着如果没有发生,则在的情况下执行此操作。它可能会加快代码的速度。。。或者它可能对性能没有影响。这取决于实际测试的复杂性,以及JIT编译器是否能够将这两个版本优化为等效的本机代码
但更重要的是,添加else
将使代码的意图更加清晰。。。假设这些都是不相交的备选方案
因此,出于这个原因,我建议无论性能如何,都添加else
。它可能会加快代码的速度。。。或者它可能对性能没有影响。这取决于实际测试的复杂性,以及JIT编译器是否能够将这两个版本优化为等效的本机代码
但更重要的是,添加else
将使代码的意图更加清晰。。。假设这些都是不相交的备选方案
因此,出于这个原因,我建议添加else
,而不管性能如何。是的。在else情况下,当发现一个条件为真时,将不计算另一个条件。如果你把它们按可爱的顺序排列,它可能会给你带来重大的影响
除此之外,每次都将对所有条件进行评估。是。在else情况下,当发现一个条件为真时,将不计算另一个条件。如果你把它们按可爱的顺序排列,它可能会给你带来重大的影响
如果没有else,则每次都将评估所有条件。如果执行最后一个else,则为否;如果发现任何中间条件为真,则为是。
但一般来说,如果条件相互排斥,则使用if-else语句更有意义。另一个选项是使用开关语句
毕竟(java)解释器就是这样工作的。逐行扫描中间字节码。。。转换为机器代码并执行它(假设我们这里不考虑JIT编译器)。因此,使用if-else比使用多个if语句更有益。如果执行最后一个else,则为否;如果发现任何中间条件为真,则为是。
但一般来说,如果条件相互排斥,则使用if-else语句更有意义。另一个选项是使用开关语句
毕竟(java)解释器就是这样工作的。逐行扫描中间字节码。。。转换为机器代码并执行它(假设我们这里不考虑JIT编译器)。因此,使用if-else比使用多个if语句更有益。如果您在比较数字,则使用开关将更有效,但使用else
已经加快了速度
想想看:在你的第一个例子中,每一个比较都完成了,即使其中一个已经被击中了。在第二个示例中,机器只进行比较,直到其中一个为真,其余的比较将不再执行
当进行更昂贵的比较时,效果可能是显著的。我曾经(~2003年)通过添加缺少的else
s,对工业机器人中的解析器进行了“优化”。它将解析配置文件的速度从超过10秒提高到不到1秒(即使“解决方案”也非常低效,但这是另一回事)。如果您比较数字,使用开关将更加有效,但是的,使用else
已经加快了速度
想想看:在你的第一个例子中,每一个比较都完成了,即使其中一个已经被击中了。在第二个示例中,机器只进行比较,直到其中一个为真,其余的比较将不再执行
当进行更昂贵的比较时,效果可能是显著的。我曾经(~2003年)通过添加缺少的else
s,对工业机器人中的解析器进行了“优化”。它将解析配置文件的速度从超过10秒提高到不到1秒(即使是“解决方案”也非常低效,但这是另一回事)。这取决于,在第一种情况下,如果您拥有所有的if,jvm将通过所有的if
在哪里
public class Test {
public static void main(String[] args) {
ifCase(5);
ifElseCase(5);
}
private static void ifCase(int x) {
long start = System.nanoTime();
if (x == 1) {
System.out.println(x);
}
if (x == 2) {
System.out.println(x);
}
if (x == 3) {
System.out.println(x);
}
if (x == 4) {
System.out.println(x);
}
if (x == 5) {
System.out.println(x);
}
if (x == 6) {
System.out.println(x);
}
if (x == 7) {
System.out.println(x);
}
if (x == 8) {
System.out.println(x);
}
if (x == 1) {
System.out.println(x);
}
if (x == 2) {
System.out.println(x);
}
if (x == 3) {
System.out.println(x);
}
if (x == 4) {
System.out.println(x);
}
if (x == 5) {
System.out.println(x);
}
if (x == 6) {
System.out.println(x);
}
if (x == 7) {
System.out.println(x);
}
if (x == 8) {
System.out.println(x);
}
if (x == 1) {
System.out.println(x);
}
if (x == 2) {
System.out.println(x);
}
if (x == 3) {
System.out.println(x);
}
if (x == 4) {
System.out.println(x);
}
if (x == 5) {
System.out.println(x);
}
if (x == 6) {
System.out.println(x);
}
if (x == 7) {
System.out.println(x);
}
if (x == 8) {
System.out.println(x);
}
if (x == 1) {
System.out.println(x);
}
if (x == 2) {
System.out.println(x);
}
if (x == 3) {
System.out.println(x);
}
if (x == 4) {
System.out.println(x);
}
if (x == 5) {
System.out.println(x);
}
if (x == 6) {
System.out.println(x);
}
if (x == 7) {
System.out.println(x);
}
if (x == 8) {
System.out.println(x);
}
long end = System.nanoTime();
System.out.println("time " + (end - start) / 1000);
}
private static void ifElseCase(int x) {
long start = System.nanoTime();
if (x == 1) {
System.out.println(x);
} else if (x == 2) {
System.out.println(x);
} else if (x == 3) {
System.out.println(x);
} else if (x == 4) {
System.out.println(x);
} else if (x == 5) {
System.out.println(x);
} else if (x == 6) {
System.out.println(x);
} else if (x == 7) {
System.out.println(x);
} else if (x == 8) {
System.out.println(x);
} else if (x == 1) {
System.out.println(x);
} else if (x == 2) {
System.out.println(x);
} else if (x == 3) {
System.out.println(x);
} else if (x == 4) {
System.out.println(x);
} else if (x == 5) {
System.out.println(x);
} else if (x == 6) {
System.out.println(x);
} else if (x == 7) {
System.out.println(x);
} else if (x == 8) {
System.out.println(x);
} else if (x == 1) {
System.out.println(x);
} else if (x == 2) {
System.out.println(x);
} else if (x == 3) {
System.out.println(x);
} else if (x == 4) {
System.out.println(x);
} else if (x == 5) {
System.out.println(x);
} else if (x == 6) {
System.out.println(x);
} else if (x == 7) {
System.out.println(x);
} else if (x == 8) {
System.out.println(x);
} else if (x == 1) {
System.out.println(x);
} else if (x == 2) {
System.out.println(x);
} else if (x == 3) {
System.out.println(x);
} else if (x == 4) {
System.out.println(x);
} else if (x == 5) {
System.out.println(x);
} else if (x == 6) {
System.out.println(x);
} else if (x == 7) {
System.out.println(x);
} else if (x == 8) {
System.out.println(x);
}
long end = System.nanoTime();
System.out.println("time " + (end - start) / 1000);
}