Java 里的 for (;;) 与 while (true),哪个更快?
mymbp:/Users/me/workspace/jdk8u/jdk/src
$ egrep -nr "for \\(\\s?;\\s?;". | wc -l
369
mymbp:/Users/me/workspace/jdk8u/jdk/src
$ egrep -nr "while \\(true". | wc -l
323
并没有差多少
while(1) {
/* ... */
}
首先看javac对下面俩语句的编译结果
publicvoid foo() {
int i = ;
while(true) { i++; }
}
/*
public void foo();
Code:
stack=1, locals=2, args_size=1
: iconst_0
1: istore_1
2: iinc 1, 1
5: goto 2
*/
publicvoid bar() {
int i = ;
for(;;) { i++; }
}
/*
public void bar();
Code:
stack=1, locals=2, args_size=1
: iconst_0
1: istore_1
2: iinc 1, 1
5: goto 2
*/
-End-
相关文章