`
trydofor
  • 浏览: 144925 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

while(ture) 和 for(;;)

阅读更多
多线程下的if,经常是while(...)
对于死等,在jdk中很多使用了 for(;;)
狗了一下 while(true) vs for(;;),有种说法是汇编级别,for(;;)更节省。
当然,该结论没代码,没版本,没验证方法,暂报疑惑。且22世纪了,代码可以傻些。

    while (true) { //(1)
    //for (;;) { //(2)
    //for (int i = 1; i > 0; i++) //(3)
        if (args.length == 1) break;
        if (args.length == 2) break;
    }


sun jdk 6 ,默认编译。
使用 Java Bytecode Editor和 JD 看了一下 *.class

能看到 (1)和(2) 被编译器弄成一样了。
等效于:
while ((args.length != 1) && (args.length != 2));

(3)等效于
for (int i = 1; i > 0; i++)
    if ((args.length == 1) || (args.length == 2)) break;


基本结论,
1. 个人习惯,爱用哪个用哪个,编译器都会私下做些事。
2. for (int i = 1; i > 0; i++) 不是死等,上限是正数最大值。
所以对于不太确定是否死掉的死等,这个算是活等。

分享到:
评论
2 楼 trydofor 2011-03-28  
(2)javap -c TestFor
(1)javap -c TestWhile
都得到

public static void main(java.lang.String[]);
  Code:
   0:   aload_0
   1:   arraylength
   2:   iconst_1
   3:   if_icmpne       0
   6:   return
1 楼 <>++< 2011-03-28  
while (true);       mov eax,1 
                          test eax,eax
                          je foo+23h
                          jmp foo+18h

for (;;);         jmp foo+23h   

for(; ; )效率更高应该没错, 不过可读性差了点, 改成for(; true; )可读性能好点吧

相关推荐

    Microservice-Architecture-Aligning-Principles-Practices-and-Culture.pdf

    ture. We offer this design-centric approach because, as we talked to several companies about their programs, we discovered one of the keys to their success was the willing‐ ness to not stick to a ...

    C# for CSDN 乱七八糟的看不懂

    abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit ...

    ESTIMATION OF CURVE SIMILARITY USING TURNING FUNCTIONS

    ture of most human pursuits, and the idea that people clas- sify together those things that people ¯nd similar is both intuitive and popular across a wide range of disciplines. Es- timation of di®...

    华南理工大学计算机全英班编译原理实验2

    1Complete a recursive–descent parser for TINY+. Parser gives syntax analysis to the tokens generated by the scanner. Output of the parser is an abstract syntax tree. 2Semantic analyzer builds the ...

    robotframewor中的条件判断.pdf

    实现python 中的break 功能 countinue for loop 实现 python 中的 countinue 功能 countine for loop if exit for loop if 4.evalute python 中直接调⽤eval() 的关键字 有 runkeyworks should be ture evalute ${...

    简单词法分析器C#含代码

    关键字:int char if else real for do while 数字、变量名(不使用下划线,头字母不为数字)、符号 词法分析生成*.TAKEN、*.SymbolTable两个文件。 保留部分语法分析接口,可以继续完成。 词法分析部分在Lex文件夹...

    rankingSvm

    ed question and the user profile, while ignoring the important statistical features, including the question-specific statistical fea-ture and the user-specific statistical features. Moreover, ...

    语言程序设计课后习题答案

    然后用while和do…while语句完成同样的循环。 解: for循环: for (int n = 100; n ; n += 2); while循环: int x = 100; while (n ) n += 2; do…while循环: int n = 100; do { n += 2; } while(n ); 2-14 if...

    《数据结构 1800题》

    WHILE i&lt;n BEGIN FOR j:=1 TO n DO x:=x+1;i:=i*2 END; 13. 下面程序段中带有下划线的语句的执行次数的数量级是( ) 【合肥工业大学 2001 三、1(2分)】 i:=n*n WHILE i&lt;&gt;1 DO i:=i div 2; 14. 计算机执行下面的...

    数位板压力测试

    The interface gives standard access to as many features as possible, while leaving room for future ex¬ten-sions and vendor-specific customizations. Applications should be able to get the tablet ...

Global site tag (gtag.js) - Google Analytics