Which loop has better performance? Why?

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

or

for(i=0;i<..){
    String s = some Assignment;
}
相关标签:
8条回答
  • When I'm using multiple threads (50+) then i found this to be a very effective way of handling ghost thread issues with not being able to close a process correctly ....if I'm wrong, please let me know why I'm wrong:

    Process one;
    BufferedInputStream two;
    try{
    one = Runtime.getRuntime().exec(command);
    two = new BufferedInputStream(one.getInputStream());
    }
    }catch(e){
    e.printstacktrace
    }
    finally{
    //null to ensure they are erased
    one = null;
    two = null;
    //nudge the gc
    System.gc();
    }
    
    0 讨论(0)
  • 2020-11-27 12:03

    To add on a bit to @Esteban Araya's answer, they will both require the creation of a new string each time through the loop (as the return value of the some Assignment expression). Those strings need to be garbage collected either way.

    0 讨论(0)
  • 2020-11-27 12:07

    In general I would choose the second one, because the scope of the 's' variable is limited to the loop. Benefits:

    • This is better for the programmer because you don't have to worry about 's' being used again somewhere later in the function
    • This is better for the compiler because the scope of the variable is smaller, and so it can potentially do more analysis and optimisation
    • This is better for future readers because they won't wonder why the 's' variable is declared outside the loop if it's never used later
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 12:09

    It seems to me that we need more specification of the problem.

    The

    s = some Assignment;
    

    is not specified as to what kind of assignment this is. If the assignment is

    s = "" + i + "";
    

    then a new sting needs to be allocated.

    but if it is

    s = some Constant;
    

    s will merely point to the constants memory location, and thus the first version would be more memory efficient.

    Seems i little silly to worry about to much optimization of a for loop for an interpreted lang IMHO.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题