wood burning stoves 2.0*
The moose likes Performance and the fly likes String declaration inside for-each loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "String declaration inside for-each loop" Watch "String declaration inside for-each loop" New topic
Author

String declaration inside for-each loop

Erap Estrada
Ranch Hand

Joined: Nov 08, 2006
Posts: 92
I read somewhere that it is not advisable to have a new string declaration inside a for-each loop for performance issue. It's because of the String pooling.

But I can't remember the clear explanation behind it. Can anyone please refresh my memory.

Thanks!


SCJP with 98% by...
Nitesh Kant
Bartender

Joined: Feb 25, 2007
Posts: 1638

Erap:
I read somewhere that it is not advisable to have a new string declaration inside a for-each loop for performance issue. It's because of the String pooling.

Well there is no such hard and fast rule and it entirely depends on the logic, if a new String is required for each iteration then there is no way to avoid it.
If you are re-initializing the same object again and again in a loop then it does not make sense irrespective of whether it is a String or not.


apigee, a better way to API!
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
You'd have to post the code, for people to be able to comment sensibly.

In general, if a value is invariant throughout a loop, then performance may be improved by calculating and assigning that value outside the loop, rather than inside. How much it is improved depends on what is being done; again, we'd have to see your code to be able to guess.

Against the above, though, is the principle of declaring variables in the smallest scope that they need, and keeping declaration close to use. This makes code more readable. It also helps GC.

Because the vast majority of code has no noticeable effect on performance whatsoever, I would advise you to write your code with clarity, rather than performance, in mind. So, if it's clearer with the String assignment inside the loop, do it that way. At least until: -

  • Your programs is proven to be too slow, and
  • The loop in question is proven, by careful measurement, to be a significant piece of code, affecting performance


  • Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    Anirudh Vyas
    Ranch Hand

    Joined: Oct 23, 2006
    Posts: 93
    Peter,

    I may be wrong about this, but the smallest scope got me thinking. I declared a string within that scope; guess what it was referred to by string pool, so how will it be garbage collected if i exit out of the loop, granted the referring local reference is removed ... but not the string pool reference.

    In general i would advice people to use concat ( '+' sign i meant ) in a for loop, this causes sBuffer to be created to append two literals (strings, i always say that duh! ) which is a performance and memory hit. I have seen too many people do that i their logging of methods ...


    Just avoid it.

    And anyways, i am always paranoid enough to make all my strings CONST_ ...

    Regards
    Vyas Anirudh


    Vyas, Anirudh
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    [Anirudh Vyas]: I may be wrong about this, but the smallest scope got me thinking. I declared a string within that scope; guess what it was referred to by string pool, so how will it be garbage collected if i exit out of the loop, granted the referring local reference is removed ... but not the string pool reference.

    If it's a literal, the string will never be collected (unless that class that declares is is collected). That's true regardless of where you use the literal. But why does it matter? As a literal, it only gets instantiated once, even if it appears in a loop.

    [Anirudh Vyas]: In general i would advice people to use concat ( '+' sign i meant ) in a for loop

    I think there's a missing "not" in this sentence, based on what you say next.

    There are some ways to use string concatenation with '+' in a loop that are completely performant. But in general I agree with the advice: don't do it unless you know what you're doing.

    [Anirudh Vyas]: And anyways, i am always paranoid enough to make all my strings CONST_ ...

    I'm not sure what this means. Do you give all your strings names that begin with CONST_?


    "I'm not back." - Bill Harding, Twister
    Subhendu Choudhury
    Greenhorn

    Joined: Feb 27, 2008
    Posts: 1
    Originally posted by Erap Estrada:
    I read somewhere that it is not advisable to have a new string declaration inside a for-each loop for performance issue. It's because of the String pooling.

    But I can't remember the clear explanation behind it. Can anyone please refresh my memory.

    Thanks!
    Jim Yingst
    Wanderer
    Sheriff

    Joined: Jan 30, 2000
    Posts: 18671
    Well, I can't argue with that.
    Chris Hurst
    Ranch Hand

    Joined: Oct 26, 2003
    Posts: 418
        
        2

    1) Write the code the cleanest way you can.
    2) Profile it
    3) if issue optimise it.

    The whole String + operator is a can of worms, and allow pointed out all over the place I've not seen many blogs show real world poor performance as opposed to bench marks specifically coded to show the problem.

    You say it creates an sBuffer and by that I presume you mean StringBuffer but it doesn't necessarily ... obviously in 1.5 it can create a StringBuilder which is unsynchronized but you'ed have forced it to create a StringBuffer which is, so you've added the cost of sync's in (which should still be a save vs object creation (Though that presumably in a 1.6 can use the new create on stack) particularly on 1.6 if escape analysis kicks in and optimise's out your locks ... and so on and so on etc etc )

    ... but my main reason for hating statements like always use an explicit StringBuffer rather than + is when less experienced Java programmers see stuff like this they start trying to optimise things like this with StringBuffer ...



    where the concatenation occurs at compile time NOT run time i.e. concatenation is instantaneous at run time as they are string literals but way slower if you start creating StringBuffer's to add two literals. I know my example looks wrong as normally the reason for concatentating two such strings is the string are big and it makes the code neater so logging is an area this is sometimes done (not by me :-) ) , equivalent to the C++ "String 1 " " String 2" type thing.


    "Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
    Billy Tsai
    Ranch Hand

    Joined: May 23, 2003
    Posts: 1304
    you can check the following web page out

    http://www.leepoint.net/notes-java/data/strings/96string_examples/example_arrayToString.html


    BEA 8.1 Certified Administrator, IBM Certified Solution Developer For XML 1.1 and Related Technologies, SCJP, SCWCD, SCBCD, SCDJWS, SCJD, SCEA,
    Oracle Certified Master Java EE 5 Enterprise Architect
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: String declaration inside for-each loop