String & StringBuffer again......

hi all,

after coding for 1 year with java (before c++ for years) i used the stringbuffer class for the first time a few days ago.

i implemented a search which has to do lot's (!) of string operations.

the search was very slow, so i tried stringbuffer.

i replaced and changed all the string stuff into stringbuffer stuff - but the performance was still bad...

so could the following be the problem:

stringbuffer don't know important functions like 'indexof(String)'.

so everytime i need indexof (for example), i do the toString() function...

now my first question:

is all the stringbuffer advantage gone if i use toString() at one or to places ==> how much performance will this cost (in a loop...)?

my second question:

what's faster, deleting the content of an used stringbuffer with delete(int,int) or just creating a new with new stringbuffer(String) ?

thanx a lot,

andi

[979 byte] By [BCBCBC] at [2007-9-26 1:19:30]
# 1

What you should probably do is use "javap -c" to look at the bytecode produced in your class. Then you can see exactly how many objects are being created for each way of coding the String and StringBuffer operations.

To answer your question about toString(), it creates an extra String object when called, but does not copy the characters. However, if you subsequently change the original StringBuffer, the characters in it will need to be copied. Therefore, calling toString() and then changing the StringBuffer further can have a serious impact on performance.

schapel at 2007-6-29 0:52:05 > top of Java-index,Archived Forums,Java Programming...
# 2

i think doing the toString is certainly costly, so you may want to work around it by making your own indexOf that does nothing more than you need.

i think the delete is the faster one.

Overall if you are still having performance problems do not use StringBuffer (they are synchronized as well), but build your own class on the char[] array and only convert it to a string at the very end when you're finished processing.

esmo at 2007-6-29 0:52:05 > top of Java-index,Archived Forums,Java Programming...