B
bob
Is there any way to use StringBuilder but with + signs instead of
append?
append?
Is there any way to use StringBuilder but with + signs instead of
append?
bob said:Is there any way to use StringBuilder but with + signs instead of
append?
Is there any way to use StringBuilder but with + signs instead of
append?
Roedy said:No. Java has so such syntactic sugar. However, the IntelliJ IDE will
convert from + notation to sb.append notation.
Jan said:But that + is internally realized via StringBuilder is a property
of javac with target >= 1.5 and not of the IDE.
The + in Java does already internally use StringBuilder.
There is only an issue when you want to accumulate a string
value. If you then explicitly use StringBuilder you are
faster, because you save the new StringBuilder() and toString().
So this is faster, since it uses 1 new and 1 toString():
StringBuilder buf=new StringBuilder();
for (int i=0; i<100; i++) {
buf.append(i);
buf("*");
buf.append(i);
buf.append("=");
buf.append((i*i));
buf.append("\n");
}
System.out.println(buf.toString());
Whereby this code is slower:
String res="";
for (int i=0; i<100; i++) {
res+=i+"*"+i+"="+(i*i)+"\n";
}
System.out.println(res);
It is translated to the following code by the compiler, and
thus uses 100 new and 100 toString():
String res="";
for (int i=0; i<100; i++) {
StringBuilder _buf=new StringBuilder(res);
_buf.append(i);
_buf("*");
_buf.append(i);
_buf.append("=");
_buf.append((i*i));
_buf.append("\n");
res=_buf.toString();
}
System.out.println(res);
For more information see for example here:
http://caprazzi.net/posts/java-bytecode-string-concatenation-and-stringbuilder/
So this is faster, since it uses 1 new and 1 toString():
And it also mocks about the following
Roedy said:I am not sure what you mean by "mock".
Like other ancient performance-practices that have been obsoleted by
today's compilers?
We have been told that using StringBuffer was the better alternative.
Now compilers have switched from using StringBuffer for the above
example to the unsynchronized StringBuilder. Those who have manually
used the StringBuffer have stopped the compiler for doing that for them
and must rely on the JITs lock elision algorithm.
So as long as this part of the code does not represent a critical
performance-bottleneck, I would recommend to use the simple, stupid
"slow" variant and hope for future compilers to detect and optimize that
pattern.
Is simple pattern that could be detected by tomorrows JIT-compilers and
transformed into:
If you then explicitly use StringBuilder you are
faster, because you save the new StringBuilder() and toString().
So this is faster, since it uses 1 new and 1 toString():
Stanimir said:Mon, 05 Sep 2011 05:27:15 +0200, /Jan Burse/:
The StringBuilder.toString() is really fast - that's the point, and I
don't think it is worth mentioning it.
The StringBuilder.toString() is really fast - that's the point, and
I don't think it is worth mentioning it.
Roedy said:The JVM is worried there might be encumbrances (pointers to) the char[].
Jan said:quite impossible
The problem is invoking a method of an object that has access to
the char[] and will do some write into the array between offset
and offset+count of the string.
Peter said:In any case, if Java does _not_ implement it that way, I suspect that's
different decision-making process rather than ignorance. In other
words, they have already considered whether it's a worthwhile
optimization and decided
Which strongly suggests that anyone worrying a priori
about the performance of StringBuilder before they have
demonstrated it's an actual bottleneck in their program
is wasting their time.
Jan said:for example (not exactly measurig toString()):
+=: 546 ms
StringBuilder, default initial capacity: 30ms
StringBuilder, exact initial capacity: 10ms
http://christian.bloggingon.net/arc...ile-bei-der-verwendung-von-stringbuilder.aspx
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.