?
Solved

How to speed up StringBuffer's append process?

Posted on 2006-04-11
25
Medium Priority
?
250 Views
Last Modified: 2013-12-29
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
0
Comment
Question by:jfz2004
  • 9
  • 6
  • 6
  • +2
25 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16431672
Allocate all the required space first
0
 
LVL 92

Expert Comment

by:objects
ID: 16431700
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
 

Author Comment

by:jfz2004
ID: 16431745
Thanks,

I am going to try StringBuilder.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 16431787
StringBuilder/Buffer makes no difference. It's the memory allocation that counts
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16433559
The fastest solution is an array:   char[60000*40];

;JOOP!
0
 

Author Comment

by:jfz2004
ID: 16435820
How do you insert a string into an array, though?
Thanks,

Jennifer
0
 
LVL 24

Assisted Solution

by:sciuriware
sciuriware earned 75 total points
ID: 16436082
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 75 total points
ID: 16436133

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

might be better still
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16436140
Typo - should have been

string.toCharArray()

of course
0
 

Author Comment

by:jfz2004
ID: 16436160
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16436230
You will achieve your greatest result by avoiding any resizing and thus by choosing a size to hold any possible input
0
 

Author Comment

by:jfz2004
ID: 16436354
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
 

Author Comment

by:jfz2004
ID: 16436401
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
 
LVL 28

Expert Comment

by:rrz
ID: 16437054
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
 
LVL 28

Expert Comment

by:rrz
ID: 16437076
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
 
LVL 28

Expert Comment

by:rrz
ID: 16437112
Sorry I didn't see that you are in a loop.
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 16437318
>>> 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
 
LVL 28

Expert Comment

by:rrz
ID: 16437331
>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
 

Author Comment

by:jfz2004
ID: 16437340
I will pass the big string back to client.
0
 

Author Comment

by:jfz2004
ID: 16437390
rrz@871311,

Thanks,
I am doing this:
 StringBuffer sb = new StringBuffer();
  sb.append("1zzz")
  sb.append("2www")
  sb.append("3uuuu")
  sb.append("4eeee");
0
 
LVL 28

Expert Comment

by:rrz
ID: 16437479
How are you using that in your loop you that posted above ?
0
 

Author Comment

by:jfz2004
ID: 16437505

I use it this way:

 resultString.Append(value)
0
 
LVL 28

Expert Comment

by:rrz
ID: 16437531
Did you try CEHJ  idea ?
0
 

Author Comment

by:jfz2004
ID: 16437591
not yet.
I am going to try it.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16446285
:-)
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question