*
The moose likes Performance and the fly likes StringBuffer instantiation in a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "StringBuffer instantiation in a loop" Watch "StringBuffer instantiation in a loop" New topic
Author

StringBuffer instantiation in a loop

Vincent Tomas
Greenhorn

Joined: Jan 16, 2007
Posts: 15
Hello,

Would there be any problems (i.e.efficiency) with instantiating a StringBuffer in a loop?

For example:



If there are any problems, would you please let me know of a better solution.

Thank you in advance,

Vincent
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Hi Vincent,

Welcome to JavaRanch!

Loops don't present any special performance issues other than that the code inside of them will be executed more than once. If the loop will run 3 or 10 or 20 times, it hardly matters. If a loop is going to run a million times, then it can be worth the effort to squeeze every possible object instantiation -- explicit or implicit -- outside of the loop.

If you tell us what kind of code you're talking about, we might have some more specific advice.


[Jess in Action][AskingGoodQuestions]
Vincent Tomas
Greenhorn

Joined: Jan 16, 2007
Posts: 15
Thank you Ernest for the prompt response! Wow!

My loop will loop 10,000 times. For example:

NOTE: this is a simplistic way of to convey my example. I would do it all in one method



Thanks!

Vincent
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

As always, the devil is in the details, but...

If what you need is 10,000 Strings that consist of two components, then you don't have much choice, really, other than using a StringBuffer (actually, in new code, Java 5's StringBuilder is a better choice, but the APIs are identical). You could use a char[] and do the moral equivalent of append() yourself, but it would be unlikely to be worth the effort.

But if you're doing something further with 'color', such that you don't really need it as a String itself -- for example, just printing it -- then it would be better to avoid the StringBuffer here.

So I guess I'm saying that we still need more information to give a really good answer.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Note that you can simply code this as



without any performance impact. The compiler will translate that to using a StringBuilder/Buffer, anyway.

Using StringBuilder only starts to pay back when you append *to the same string* *in more than one statement*, i.e. while looping.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StringBuffer instantiation in a loop
 
Similar Threads
for/in - developer's notebook
new to working with arrays
Avoiding memory allocation?
How to append in excel sheet using Java
Out of memory