Loop algorithm

I got the following code:

Vector

Vector<String> v =new Vector<String>();

for (int i = 0; i < 10000; ++i)// For example

{

v.add(str1 + str2 + str3);

}

where st1, str2 and str3 are strings that change through every iteration, so they can't be added from before.

The adding line is quite a performance killer, hprof:heap=sites returned me this:

SITES BEGIN (ordered by live bytes) Thu Jan 19 14:14:10 2006

percent live alloc'ed stack class

rankself accumbytes objsbytes objs trace name

1 12.77% 12.77%245248 3961 27040000 454000 305536 char[]

..............................................................................................................................

and

TRACE 305536:

java.lang.String.<init>(String.java:208)

java.lang.StringBuilder.toString(StringBuilder.java:431)

[The adding line]

Does anyone know whats wrong with that code, because in my program, the line is quite a performance killer. I already tried to build a StringBuffer and add it afterwards, but it didn't bring any improvement.

Does anybody know a suitable solution?

R. Hollenstein

[1406 byte] By [r.hollensteina] at [2007-10-2 10:07:10]
# 1

If you have to concatenate the strings and put them in the array, then you will incur the cost of concatenating the strings.

If you don't have to concatenate the strings, or can do so in a lazy manner, then there are ways round it, but that requires knowing what str1,2,3 mean and what the array is used for.

Pete

pm_kirkhama at 2007-7-13 1:24:31 > top of Java-index,Other Topics,Algorithms...