You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jmeter.apache.org by Felix Schumacher <fe...@internetallee.de> on 2014/12/28 13:34:48 UTC
StringBuilder against compile time String concatenation
Hi all,
in XPathUtil I have found the following code fragment:
log.debug(new
StringBuilder("bla").append(blub).append("whatever").toString())
which seems to me t be equivallent to
log.debug("bla"+blubb+"whatever")
any reason to use the former?
Regards
Felix
Re: StringBuilder against compile time String concatenation
Posted by sebb <se...@gmail.com>.
On 28 December 2014 at 12:34, Felix Schumacher
<fe...@internetallee.de> wrote:
> Hi all,
>
> in XPathUtil I have found the following code fragment:
>
> log.debug(new
> StringBuilder("bla").append(blub).append("whatever").toString())
>
> which seems to me t be equivallent to
>
> log.debug("bla"+blubb+"whatever")
>
> any reason to use the former?
I think originally the Java compiler was not clever enough to convert
mixed string and variable concatenation into a sequence of
StringBuilder.append calls.
I suspect the code originally used StringBuffer, which was then
converted to StringBuilder.
I agree there is no need to use explicit StringBuilder calls here.
However the log.debug call should be enclosed by
if (log.isDebugEnabled()) { }
if it is not already.
> Regards
> Felix
Re: StringBuilder against compile time String concatenation
Posted by chaitanya bhatt <bh...@gmail.com>.
Brilliant analysis!
Chaitanya M Bhatt
http://www.performancecompetence.com
On Wed, Dec 31, 2014 at 12:26 PM, Felix Schumacher <
felix.schumacher@internetallee.de> wrote:
> Am 31.12.2014 um 19:15 schrieb chaitanya bhatt:
>
>> Concatenation with + operator will be internally converted to a
>> StringBuffer( as synchronized version of StringBuilder class). IMO there
>> isn't much performance difference unless you are trying to concatenate in
>> a
>> loop, in such cases StringBuilder would be a better option.
>>
> No, the example I stated was not a loop, but quite static. At least java 6
> seems to compile such static "+" operators to StringBuilder. That is what
> javap -c shows on my test classes. The jls for java5 cited below states,
> that it may be converted to a StringBuffer, but that that is left to the
> compiler. Seems the compiler people moved on.
>
>>
>> Also, since StringBuffer is synchronized, depending on how you are using
>> the String object in the VM StringBuilder may outperform "+" approach.
>> (Take a look a the StackOverflow discussion)
>>
> I have done a simple microbenchmark, which shows the simple + operator to
> be the fasted of the three variants "+", StringBuilder and StringBuffer,
> with StringBuilder being just a bit slower. Which is probably more luck
> than anything else, since the bytecode decompiles to the same instructions.
> StringBuffer is slowest (as expected, but not that much) (Code compiled
> with java 6 and run with Java 6, 7 and 8)
>
>>
>> Also, I feel StringBuilder improves readability of a code.
>>
> I feel a simple "+" is more readable than a constructor plus two or more
> calls of append. But that is why I asked :)
>
> Regards
> Felix
>
>
>> Plus,
>>
>> http://docs.oracle.com/javase/specs/jls/se5.0/html/
>> expressions.html#15.18.1.2
>> http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer
>>
>>
>> Chaitanya M Bhatt
>> http://www.performancecompetence.com
>>
>> On Sun, Dec 28, 2014 at 4:34 AM, Felix Schumacher <
>> felix.schumacher@internetallee.de> wrote:
>>
>> Hi all,
>>>
>>> in XPathUtil I have found the following code fragment:
>>>
>>> log.debug(new StringBuilder("bla").append(blub).append("whatever").
>>> toString())
>>>
>>> which seems to me t be equivallent to
>>>
>>> log.debug("bla"+blubb+"whatever")
>>>
>>> any reason to use the former?
>>>
>>> Regards
>>> Felix
>>>
>>>
>
Re: StringBuilder against compile time String concatenation
Posted by Felix Schumacher <fe...@internetallee.de>.
Am 31.12.2014 um 19:15 schrieb chaitanya bhatt:
> Concatenation with + operator will be internally converted to a
> StringBuffer( as synchronized version of StringBuilder class). IMO there
> isn't much performance difference unless you are trying to concatenate in a
> loop, in such cases StringBuilder would be a better option.
No, the example I stated was not a loop, but quite static. At least java
6 seems to compile such static "+" operators to StringBuilder. That is
what javap -c shows on my test classes. The jls for java5 cited below
states, that it may be converted to a StringBuffer, but that that is
left to the compiler. Seems the compiler people moved on.
>
> Also, since StringBuffer is synchronized, depending on how you are using
> the String object in the VM StringBuilder may outperform "+" approach.
> (Take a look a the StackOverflow discussion)
I have done a simple microbenchmark, which shows the simple + operator
to be the fasted of the three variants "+", StringBuilder and
StringBuffer, with StringBuilder being just a bit slower. Which is
probably more luck than anything else, since the bytecode decompiles to
the same instructions. StringBuffer is slowest (as expected, but not
that much) (Code compiled with java 6 and run with Java 6, 7 and 8)
>
> Also, I feel StringBuilder improves readability of a code.
I feel a simple "+" is more readable than a constructor plus two or more
calls of append. But that is why I asked :)
Regards
Felix
>
> Plus,
>
> http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.18.1.2
> http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer
>
>
> Chaitanya M Bhatt
> http://www.performancecompetence.com
>
> On Sun, Dec 28, 2014 at 4:34 AM, Felix Schumacher <
> felix.schumacher@internetallee.de> wrote:
>
>> Hi all,
>>
>> in XPathUtil I have found the following code fragment:
>>
>> log.debug(new StringBuilder("bla").append(blub).append("whatever").
>> toString())
>>
>> which seems to me t be equivallent to
>>
>> log.debug("bla"+blubb+"whatever")
>>
>> any reason to use the former?
>>
>> Regards
>> Felix
>>
Re: StringBuilder against compile time String concatenation
Posted by chaitanya bhatt <bh...@gmail.com>.
Concatenation with + operator will be internally converted to a
StringBuffer( as synchronized version of StringBuilder class). IMO there
isn't much performance difference unless you are trying to concatenate in a
loop, in such cases StringBuilder would be a better option.
Also, since StringBuffer is synchronized, depending on how you are using
the String object in the VM StringBuilder may outperform "+" approach.
(Take a look a the StackOverflow discussion)
Also, I feel StringBuilder improves readability of a code.
Plus,
http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.18.1.2
http://stackoverflow.com/questions/355089/stringbuilder-and-stringbuffer
Chaitanya M Bhatt
http://www.performancecompetence.com
On Sun, Dec 28, 2014 at 4:34 AM, Felix Schumacher <
felix.schumacher@internetallee.de> wrote:
> Hi all,
>
> in XPathUtil I have found the following code fragment:
>
> log.debug(new StringBuilder("bla").append(blub).append("whatever").
> toString())
>
> which seems to me t be equivallent to
>
> log.debug("bla"+blubb+"whatever")
>
> any reason to use the former?
>
> Regards
> Felix
>