Link to home
Start Free TrialLog in
Avatar of jfz2004
jfz2004

asked on

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Allocate all the required space first
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)
Avatar of jfz2004
jfz2004

ASKER

Thanks,

I am going to try StringBuilder.
StringBuilder/Buffer makes no difference. It's the memory allocation that counts
The fastest solution is an array:   char[60000*40];

;JOOP!
Avatar of jfz2004

ASKER

How do you insert a string into an array, though?
Thanks,

Jennifer
SOLUTION
Avatar of sciuriware
sciuriware

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Typo - should have been

string.toCharArray()

of course
Avatar of jfz2004

ASKER

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.

You will achieve your greatest result by avoiding any resizing and thus by choosing a size to hold any possible input
Avatar of jfz2004

ASKER

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
                {
                   
                }
               
            }
}
Avatar of jfz2004

ASKER

objects,

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

Jennifer
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");
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");
Sorry I didn't see that you are in a loop.
>>> 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!
>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 ?
Avatar of jfz2004

ASKER

I will pass the big string back to client.
Avatar of jfz2004

ASKER

rrz@871311,

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

ASKER


I use it this way:

 resultString.Append(value)
Did you try CEHJ  idea ?
Avatar of jfz2004

ASKER

not yet.
I am going to try it.
:-)