Which costs more while looping; assignment or an if-statement?

前端 未结 6 1267
不知归路
不知归路 2020-12-29 06:04

Consider the following 2 scenarios:

boolean b = false;
int i = 0;
while(i++ < 5) {
    b = true;
}

OR

boolean b = false;         


        
6条回答
  •  一向
    一向 (楼主)
    2020-12-29 06:48

    Please do not forget the rules of Optimization Club.

    1. The first rule of Optimization Club is, you do not Optimize.
    2. The second rule of Optimization Club is, you do not Optimize without measuring.
    3. If your app is running faster than the underlying transport protocol, the optimization is over.
    4. One factor at a time.
    5. No marketroids, no marketroid schedules.
    6. Testing will go on as long as it has to.
    7. If this is your first night at Optimization Club, you have to write a test case.

    It seems that you have broken rule 2. You have no measurement. If you really want to know, you'll answer the question yourself by setting up a test that runs scenario A against scenario B and finds the answer. There are so many differences between different environments, we can't answer.

提交回复
热议问题