Java for 循环与 while 循环。性能差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1165457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java for loop vs. while loop. Performance difference?
提问by
Assume i have the following code, there are three for loop to do something. Would it run fast if i change the most outer for loop to while loop? thanks~~
假设我有以下代码,有三个 for 循环来做某事。如果我将最外层的 for 循环更改为 while 循环,它会运行得很快吗?谢谢~~
int length = 200;
int test = 0;
int[] input = new int[10];
for(int i = 1; i <= length; i++) {
for (int j = 0; j <=length - i; j++) {
for (int k = 0; k < length - 1; k++) {
test = test + input[j + k];
}
}
}
回答by jjnguy
No, changing the type of loop wouldn't matter.
不,更改循环类型无关紧要。
The only thing that can make it faster would be to have less nesting of loops, and looping over less values.
唯一可以使它更快的方法是减少嵌套循环,并循环使用更少的值。
The only difference between a for
loop and a while
loop is the syntax for defining them. There is no performance difference at all.
for
循环和while
循环之间的唯一区别是定义它们的语法。根本没有性能差异。
int i = 0;
while (i < 20){
// do stuff
i++;
}
Is the same as:
是相同的:
for (int i = 0; i < 20; i++){
// do Stuff
}
(Actually the for-loop is a little better because the i
will be out of scope after the loop while the i
will stick around in the while
loop case.)
(实际上 for 循环要好一些,因为循环i
后将超出范围,而i
将留在while
循环情况下。)
A for loop is just a syntactically prettier way of looping.
for 循环只是一种语法上更漂亮的循环方式。
回答by aquinas
No, you're still looping the exact same number of times. Wouldn't matter at all.
不,您仍然循环完全相同的次数。根本没有关系。
回答by Extrakun
It would only matter if you are using multi-thread or multiple processor programming. Then it would also depends on how you assign the loops to the various processors/threads.
仅当您使用多线程或多处理器编程时才重要。然后它还取决于您如何将循环分配给各种处理器/线程。
回答by Roalt
Look at your algorithm! Do you know beforehand which values from your array are added more than one time?
看看你的算法!您是否事先知道数组中的哪些值被添加了多次?
If you know that you could reduce the number of loops and that would result in better performance.
如果您知道可以减少循环次数,从而提高性能。
回答by ufukgun
you cant optimize it by changing it to while.
您无法通过将其更改为 while 来优化它。
you can just increment speed very very very very little by changing the line
你可以通过改变线路来非常非常非常非常小地增加速度
for (int k = 0; k < length - 1; k++) {
by
经过
for (int k = 0; k < lengthMinusOne; k++) {
where lengthMinusOne is calculated before
其中 lengthMinusOne 之前计算过
this subtraction is just calculating almost (200x201/2) x (200-1) times and it is very little number for computer :)
这个减法只是计算几乎 (200x201/2) x (200-1) 次,对于计算机来说是非常少的数字:)
回答by Bombe
This kind of micro-optimization is pointless.
这种微优化毫无意义。
- A while-loop won't be faster.
- The loop structure is notyour bottleneck.
- Optimize your algorithm first.
- Better yet, don't optimize first. Only optimize after you have found out that you really have a bottleneck in your algorithm that is not I/O-dependant.
- while 循环不会更快。
- 循环结构不是你的瓶颈。
- 首先优化你的算法。
- 更好的是,不要先优化。只有在您发现算法中确实存在不依赖于 I/O 的瓶颈后才进行优化。
回答by fortran
Even if the hypothesis of the while loop being faster than the for loop were true (and it's not), the loops you'd had to change/optimize wouldn't be the outer ones but the inner ones, because those are executed more times.
即使 while 循环比 for 循环更快的假设是正确的(事实并非如此),您必须更改/优化的循环不会是外部循环而是内部循环,因为这些循环执行的次数更多.
回答by Clement Herreman
The difference between for and while is semantic:
for 和 while 之间的区别在于语义:
- In a while loop, you will loop as long as the condition is true, which can vary a lot, because you might, in your loop, modify variables using in evluating the while condition.
- Usually, in a for loop, you loop N time. This N can be variable, but doesn't move until the end of your N loop, as usually developpers doesn't modify variables evaluated in the loop condition.
- 在 while 循环中,只要条件为真,您就会循环,这可能会有很大差异,因为您可能会在循环中修改用于评估 while 条件的变量。
- 通常,在 for 循环中,循环 N 次。这个 N 可以是变量,但直到 N 循环结束才会移动,因为通常开发人员不会修改在循环条件中评估的变量。
It is a way to help other to understand your code. You are not obliged not to modify for loop variables, but it is a common (and good) practice.
这是一种帮助他人理解您的代码的方式。您没有义务不修改 for 循环变量,但这是一种常见(且良好)的做法。
回答by Adrian
There would be no performance difference. Try it out!
不会有性能差异。试试看!
The JVM and further, the compiler, would make both loops into something like
JVM 以及编译器会将两个循环都变成类似
label:
;code inside your for loop.
LOOP label
回答by levtatarov
here's a helpful linkto an article on the matter
这是一篇关于此事的文章的有用链接
according to it, the While and For are almost twice as faster but both are the same.
根据它,While 和 For 几乎快两倍,但两者都是相同的。
BUTthis article was written in 2009 and so i tried it on my machine and here are the results:
但是这篇文章是在 2009 年写的,所以我在我的机器上试了一下,结果如下:
- using java 1.7: the Iterator was about 20%-30% faster than For and While (which were still the same)
- using java 1.6: the Iterator was about 5% faster than For and While (which were still the same)
- 使用 java 1.7:迭代器比 For 和 While(仍然相同)快约 20%-30%
- 使用 java 1.6:迭代器比 For 和 While(仍然相同)快约 5%
so i guess the best thing is to just time it on your own version and machine and conclude from that
所以我想最好的办法就是在你自己的版本和机器上计时,然后得出结论