[PJUG Javamail] StringBuilder vs +
Douglas Bullard
dbullard at nurflugel.com
Fri Jun 19 01:10:11 EDT 2009
True, they were constants, but I've seen a LOT of stuff build up with
constants using StringBuilder/Buffer.
My other point is that since the compiler does it all with
StringBuilder anyway, make your code more readable. Here's a nice,
dense bit of code someone wrote:
StringBuilder sb = new StringBuilder();
sb
.append
("select
").append(id).append(i).append("=").append(cmbnames[i]).append(";\n");
sb.append("selname").append(id).append("[").append(i).append("]=
\"").append("select").append(
id).append(
i).append(
"\";\n");
sb.append("func").append(id).append(i).append("=new Function();\n");
sb.append("if(select").append(id).append(i).append(".onchange) {\n");
sb
.append
("func
").append(id).append(i).append("=select").append(id).append(i).append(
".onchange;\n}\n");
sb.append("function refill").append(id).append(i).append("() {\n");
sb
.append("select").append(id).append("=").append(cmbnames[i]).append(";
\n");
sb.append("\nrefill").append(id).append("(").append(i).append(",
").append("select").append(
id).append(
".selectedIndex); ");
sb.append("\nfunc").append(id).append(i).append("();\n");
sb.append("}\n");
String result=sb.toString();
You can definitely make it easier to read (and a tiny bit faster) by
using the "+".
Doug
On Jun 18, 2009, at 11:48, Joe Sam Shirah wrote:
>
> Hi Doug,
>
> Just a note that you used all constants in your test. String
> constants
> are interned, and what you stated as obvious is true. However, it
> seems to
> me that a real test should eliminate the ability to concatenate at
> compile
> time and use run time variables instead. I think that would reflect
> more
> real world situations. There are several things like this that run
> better
> in certain situations given how Java reacts.
>
> I don't have time to run the tests myself now, and you may get
> similar
> results, but I think it's worth doing.
>
>
> Joe Sam
>
> Joe Sam Shirah - http://www.conceptgo.com
> conceptGO - Consulting/Development/Outsourcing
> Java Filter Forum: http://www.ibm.com/developerworks/java/
> Just the JDBC FAQs: http://www.jguru.com/faq/JDBC
> Going International? http://www.jguru.com/faq/I18N
> Que Java400? http://www.jguru.com/faq/Java400
>
> ----- Original Message -----
> From: "Bullard, Douglas" <Douglas.Bullard at nike.com>
> To: <Javamail at pjug.org>
> Sent: Thursday, June 18, 2009 2:19 PM
> Subject: [PJUG Javamail] StringBuilder vs +
>
>
> Good presentation by Chris Cowell-Shah the other night.
>
>
>
> I'd mentioned that I'd seen that building simple strings with the "+"
> sign was faster then using StringBuilder, and a lot of people
> challenged
> that, so I wrote a little test to see if my memory was faulty.
>
>
>
> I did three comparisons, warming up the VM so that any HotSpot
> optimizations had taken place and the VM had stabilized. I used "+",
> StringBuffer, and StringBuilder, for JDK 1.6, 1.5, and 1.4, running on
> Windows.
>
>
>
> Here are the results:
>
> Test
>
> JDK 1.6
>
> JDK 1.5
>
> JDK 1.4
>
> StringBuffer
>
> 734
>
> 828
>
> 1375
>
> StringBuilder
>
> 734
>
> 828
>
> ----
>
> +
>
> 687
>
> 766
>
> 969
>
>
>
> This confirmed my results, although the other guys were right - the
> compiler does use StringBuilder internally.
>
>
>
> Here is the guts of the test for StringBuilder - I create the string,
> then add it to a list so it has to hand around:
>
> for (int i = 0; i < numberOfLoops; i++){
>
> StringBuilder sb=new StringBuilder();
>
>
> sb
> .append("This").append("is").append("a").append("test").append("string
> ").append(i);
>
> list.add(sb.toString());
>
>
>
> The concatenation approach is the same:
>
>
>
> for (int i = 0; i < numberOfLoops; i++){
>
> String temp = "This " + "is " + "a " + "test " + "string" + i;
>
> list.add(temp);
>
>
>
>
>
> So, why the difference? Looking at the bytecode, it's obvious. The
> compiler optimizes the concatenation of the "+" strings into a single
> string at compile time - "This is a test string", then calls
> StringBuilder to append the value of i.
>
>
>
> Using a pure StringBuilder approach, it has to make multiple calls to
> the append method at runtime.
>
>
>
> The difference in performance is much less with JDK 1.6, but it's nice
> to see that visually cleaner code is also more performant. We see
> this
> a lot when people build up log messages, etc, and feel obligated to
> use
> StringBuilder to make it "fast", and the result is a whole bunch of
> ugly
> code. I like letting the compiler do the heavy lifting.
>
>
>
>
>
> Douglas Bullard
>
>
>
>
>
>
>
>
>
>
>
>
> --------------------------------------------------------------------------------
>
>
>> _______________________________________________
>> Web Site - http://www.pjug.org/
>> Javamail mailing list
>> Javamail at pjug.org
>> http://www.pjug.org/mailman/listinfo/javamail
>>
>
> _______________________________________________
> Web Site - http://www.pjug.org/
> Javamail mailing list
> Javamail at pjug.org
> http://www.pjug.org/mailman/listinfo/javamail
>
>
More information about the Javamail
mailing list