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
So I use StringBuffer's append to achieve it. But it takes too long.
How to speed it up?
Thanks,
Jennifer
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)
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html#StringBuilder(int)
ASKER
Thanks,
I am going to try StringBuilder.
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!
;JOOP!
ASKER
How do you insert a string into an array, though?
Thanks,
Jennifer
Thanks,
Jennifer
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Typo - should have been
string.toCharArray()
of course
string.toCharArray()
of course
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.
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
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
{
}
}
}
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
{
}
}
}
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
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");
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");
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.to CharArray( ), 0, array, currentIndex, string.length()];
might be the fastest solution.
;JOOP!
>>> 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.to
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 ?
What are you going to do with this big string ?
ASKER
I will pass the big string back to client.
ASKER
rrz@871311,
Thanks,
I am doing this:
StringBuffer sb = new StringBuffer();
sb.append("1zzz")
sb.append("2www")
sb.append("3uuuu")
sb.append("4eeee");
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 ?
ASKER
I use it this way:
resultString.Append(value)
Did you try CEHJ idea ?
ASKER
not yet.
I am going to try it.
I am going to try it.
:-)