How to speed up StringBuffer's append process?

Hi, I need to create a new string by concatenating 60000*40 sub strings.
So I use StringBuffer's append to achieve it. But it takes too long.

How to speed it up?

Thanks,

Jennifer
jfz2004Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Allocate all the required space first
0
objectsCommented:
Try a StringBuilder with the space required allocated up front

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html#StringBuilder(int)
0
jfz2004Author Commented:
Thanks,

I am going to try StringBuilder.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

CEHJCommented:
StringBuilder/Buffer makes no difference. It's the memory allocation that counts
0
sciuriwareCommented:
The fastest solution is an array:   char[60000*40];

;JOOP!
0
jfz2004Author Commented:
How do you insert a string into an array, though?
Thanks,

Jennifer
0
sciuriwareCommented:
char array[2400000];
int index;
...............         // index has a value < 2400000.

String k = ............. // a new String;

for(int i = 0;  i < k.length();  ++i)
{
     array[index++] = k.charAt(i);
}


;JOOP!
0
CEHJCommented:

System.arraycopy[string.toCharArray, 0, array, currentIndex, string.length()];

might be better still
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
CEHJCommented:
Typo - should have been

string.toCharArray()

of course
0
jfz2004Author Commented:
sciuriware,
Thanks for the help. But I still can't use char[] since I don't know the initial
size. 600000 is an estimated number.

Thanks anyway.

0
CEHJCommented:
You will achieve your greatest result by avoiding any resizing and thus by choosing a size to hold any possible input
0
jfz2004Author Commented:
Thanks,

Basically, I have a sql statement and I use the sql to oracle DB to pull a result set.
Then, I loop through the rows in the result set and put all the result into one big string. But at the begining, I don't know how many rows I will get.

StringBuffer  resultString
while (rs.next())
 {
   
            for (int i = 1; i <= colCount; i++)
            {
             
 
                if (value != null)
                {
                   resultString.Append(value)
                }
                else
                {
                   
                }
               
            }
}
0
jfz2004Author Commented:
objects,

Thanks for your idea of StringBuilder. But since I am using Java JDK 1.4.2,
I can't use StringBuilder, neither.

Jennifer
0
rrzCommented:
Here is a trick I read about but I never tried it.  

  StringBuffer sb = new StringBuffer();
  sb.append("1zzz")
    .append("2www")
    .append("3uuuu")
    .append("4eeee");

Are you donig that already or are doing  this ?
  StringBuffer sb = new StringBuffer();
  sb.append("1zzz")
  sb.append("2www")
  sb.append("3uuuu")
  sb.append("4eeee");
0
rrzCommented:
I meant  
Are you donig that already or are doing  this ?
  StringBuffer sb = new StringBuffer();
  sb.append("1zzz");
  sb.append("2www");
  sb.append("3uuuu");
  sb.append("4eeee");
0
rrzCommented:
Sorry I didn't see that you are in a loop.
0
sciuriwareCommented:
>>> sciuriware,
>>> Thanks for the help. But I still can't use char[] since I don't know the initial
>>> size. 600000 is an estimated number.
It's your info that you need 60000*40 positions.
There's nothing wrong with an array of 1000000000 positions if that's bigger than you'll ever need.
But resizing is a bigger consumer than over-allocation!

CEHJ's  System.arraycopy[string.toCharArray(), 0, array, currentIndex, string.length()];
might be the fastest solution.
;JOOP!
0
rrzCommented:
>Then, I loop through the rows in the result set and put all the result into one big string  
What are you going to do with this big string ?
0
jfz2004Author Commented:
I will pass the big string back to client.
0
jfz2004Author Commented:
rrz@871311,

Thanks,
I am doing this:
 StringBuffer sb = new StringBuffer();
  sb.append("1zzz")
  sb.append("2www")
  sb.append("3uuuu")
  sb.append("4eeee");
0
rrzCommented:
How are you using that in your loop you that posted above ?
0
jfz2004Author Commented:

I use it this way:

 resultString.Append(value)
0
rrzCommented:
Did you try CEHJ  idea ?
0
jfz2004Author Commented:
not yet.
I am going to try it.
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.