Which loop has better performance? Why?

后端 未结 8 1648
粉色の甜心
粉色の甜心 2020-11-27 11:24
String s = \"\";
for(i=0;i<....){
    s = some Assignment;
}

or

for(i=0;i<..){
    String s = some Assignment;
}
8条回答
  •  隐瞒了意图╮
    2020-11-27 12:07

    If you want to speed up for loops, I prefer declaring a max variable next to the counter so that no repeated lookups for the condidtion are needed:

    instead of

    for (int i = 0; i < array.length; i++) {
      Object next = array[i];
    }
    

    I prefer

    for (int i = 0, max = array.lenth; i < max; i++) {
      Object next = array[i];
    }
    

    Any other things that should be considered have already been mentioned, so just my two cents (see ericksons post)

    Greetz, GHad

提交回复
热议问题