Yes, it's very likely that the string concatenation is impacting performance especially if the strings being concatenated are long and/or there are many of them.
You should avoid using "+" for string concatenation especially in loops. The above solution will be much faster, but you can further improve execution time by eliminating all string concatenation:
Strings are immutable so each time you do concatenation, a new String must be created that is big enough to hold both Strings. StringBuffer and StringBuilder don't need to allocate a new buffer each time something is appended.
You should avoid using "+" for string concatenation especially in loops.
Well, you should use "+" where appropriate. Have a look at this article for a more nuanced view of where to use "+". I'll bet that using System.out.println() is as big a performance drag as "+" in the above code as it blocks until a call out to the native console returns. Again, this would be an interesting thing to test and measure. [ August 09, 2006: Message edited by: Joe Ess ]
This looks pretty small and easy to convert to StringBuilder and in a separate test just remove the system.out call and see what happens. I know I had some thigns that seemed to crawl with displays like that and I changed them to log every 1000 times through or 10k or whatever for much better speed. Let us know what works!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi