Java for循环与while循环。 性能差异?
假设我有以下代码,有三个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]; } } }
不,改变循环的类型无关紧要。
唯一可以使它更快的事情是减少循环嵌套,并循环使用较少的值。
for
循环和while
循环之间的唯一区别是定义它们的语法。 根本没有性能差异。
int i = 0; while (i < 20){ // do stuff i++; }
是相同的:
for (int i = 0; i < 20; i++){ // do Stuff }
(实际上for循环有点好,因为在循环之后i
将超出范围,而i
将在while
循环情况下while
不变。)
for循环只是一种语法更漂亮的循环方式。
这种微观优化毫无意义。
- while循环不会更快。
- 循环结构不是你的瓶颈。
- 首先优化您的算法。
- 更好的是,不要先优化。 只有在您发现算法确实存在不依赖于I / O的瓶颈后才进行优化。
你不能通过改变它来优化它。
你可以通过改变线来非常非常非常地增加速度
for (int k = 0; k < length - 1; k++) {
通过
for (int k = 0; k < lengthMinusOne; k++) {
其中lengthMinusOne之前计算过
这个减法只是计算几乎(200x201 / 2)x(200-1)次,这对于计算机来说是非常少的数字:)
有人建议测试while
和for
循环,所以我创建了一些代码来测试while循环或for循环是否更快; 平均而言,超过100,000次测试, while
循环更快〜95%的时间。 我可能编码不正确,我对编码很新,还考虑到我是否只运行了10,000个循环,它们最终在运行持续时间内非常均匀。
编辑当我去测试更多试验时,我没有移动所有数组值。 修正了它,以便更改您运行的试验次数。
import java.util.Arrays; class WhilevsForLoops { public static void main(String[] args) { final int trials = 100; //change number of trials final int trialsrun = trials - 1; boolean[] fscount = new boolean[trials]; //faster / slower boolean int p = 0; // while counter variable for for/while timers while (p <= trialsrun) { long[] forloop = new long[trials]; long[] whileloop = new long[trials]; long systimeaverage; long systimenow = System.nanoTime(); long systimethen = System.nanoTime(); System.out.println("For loop time array : "); for (int counter=0;counter <= trialsrun; counter++) { systimenow = System.nanoTime(); System.out.print(" #" + counter + " @"); systimethen = System.nanoTime(); systimeaverage = (systimethen - systimenow); System.out.print( systimeaverage + "ns |"); forloop[counter] = systimeaverage; } int count = 0; System.out.println(" "); System.out.println("While loop time array: "); while (count <= trialsrun) { systimenow = System.nanoTime(); System.out.print(" #" + count + " @"); systimethen = System.nanoTime(); systimeaverage = (systimethen - systimenow); System.out.print( systimeaverage + "ns |"); whileloop[count] = systimeaverage; count++; } System.out.println("==============================================="); int sum = 0; for (int i = 0; i <= trialsrun; i++) { sum += forloop[i]; } System.out.println("for loop time average: " + (sum / trials) + "ns"); int sum1 = 0; for (int i = 0; i <= trialsrun; i++) { sum1 += whileloop[i]; } System.out.println("while loop time average: " + (sum1 / trials) + "ns"); int longer = 0; int shorter = 0; int gap = 0; sum = sum / trials; sum1 = sum1 / trials; if (sum1 > sum) { longer = sum1; shorter = sum; } else { longer = sum; shorter = sum1; } String longa; if (sum1 > sum) { longa = "~while loop~"; } else { longa = "~for loop~"; } gap = longer - shorter; System.out.println("The " + longa + " is the slower loop by: " + gap + "ns"); if (sum1 > sum) { fscount[p] = true; } else { fscount[p] = false; } p++; } int forloopfc=0; int whileloopfc=0; System.out.println(Arrays.toString(fscount)); for(int k=0; k <= trialsrun; k++) { if (fscount[k] == true) { forloopfc++; } else { whileloopfc++;} } System.out.println("--------------------------------------------------"); System.out.println("The FOR loop was faster: " + forloopfc + " times."); System.out.println("The WHILE loop was faster: " + whileloopfc + " times."); } }
即使while循环的假设比for循环更快是假的(并且它不是),你必须改变/优化的循环不会是外部循环而是内部循环,因为这些循环执行的次数更多。
for和while之间的区别是语义 :
- 在while循环中,只要条件为真,就会循环,这可能会有很大差异,因为在循环中,您可能会在使用while条件时修改变量。
- 通常,在for循环中,循环N次。 这个N可以是变量,但是在N循环结束之前不会移动,因为通常开发人员不会修改在循环条件中评估的变量。
这是一种帮助其他人理解您的代码的方法。 您没有义务不修改循环变量,但这是一种常见(和良好)的做法。
这是关于此事的文章的有用链接
根据它,While和For几乎快两倍,但两者都是相同的。
但是这篇文章是在2009年写的,所以我在我的机器上尝试了它,结果如下:
- 使用java 1.7:Iterator比For和While快20%-30%(仍然相同)
- 使用java 1.6:Iterator比For和While快了大约5%(仍然是相同的)
所以我想最好的事情就是把它放在你自己的版本和机器上,并从中得出结论
不,你仍然循环完全相同的次数。 根本没关系。
看看你的算法吧! 您是否事先知道arrays中的哪些值被添加了多次?
如果您知道可以减少循环次数,那么可以获得更好的性能。
只有使用multithreading或多处理器编程才有意义。 那么它还取决于你如何将循环分配给各种处理器/线程。
没有性能差异。 试试看!
JVM以及编译器将使两个循环成为类似的东西
label: ;code inside your for loop. LOOP label
有没有人这样试过……
int i = 20; while (--i > -1){ // do stuff }
相比:
for (int i = 0; i < 20; i++){ // do Stuff }
基于此: https : //jsperf.com/loops-analyze (不是我创建的)while循环比一般的for循环慢22%。 至少在Javascript中是这样的。