Which loop has better performance? Why?

后端 未结 8 1651
粉色の甜心
粉色の甜心 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:14

    In theory, it's a waste of resources to declare the string inside the loop. In practice, however, both of the snippets you presented will compile down to the same code (declaration outside the loop).

    So, if your compiler does any amount of optimization, there's no difference.

提交回复
热议问题