It does appear there is a small advantage to use the append(char c)
versus append(String s) for adding a single character, (i.e. doing an
append of the character ").
In the append(String s) case, a copy of the char(s) in String s are
copied from String s into the StringBuilder's (or StringBuffer's)
underlying char[]. In append(char c), the char c is assigned to next
element in the StringBuilder's (or StringBuffer's) underlying char[].
In addition, when you use "\"", you create a new String object. But,
when you use the char ' " ' you allocate a native character. The end
result is fewer String object allocations. char allocations take up
less space than String objects.
So, it appears an append('"') is a slightly better choice than append("\"");
charlie ...
Oleksiy Stashok wrote:
> I think there could be small advantage.
> As char will be directly added to StringBuffer without any additional
> actions, which probably are for String parameter.
> In any case it could be just one more small improvement :)
>
> WBR,
> Alexey.
>
>
> charlie hunt wrote:
>> Don't know ... never tried that approach nor do I know if there are
>> any performance advantages of that approach either?
>>
>> charlie ....
>>
>> Oleksiy Stashok wrote:
>>> Hello,
>>>
>>> what about
>>> response.append('"').append(value).append('"'); // double quote
>>> inside single quotes
>>>
>>> does it make any sense?
>>>
>>> Alexey.
>>>
>>> charlie hunt wrote:
>>>> Don't know if anyone else noticed hamada's commit ?
>>>>
>>>> There's a nice enhancement that's done in that commit which makes
>>>> better use of StringBufffer.append().
>>>>
>>>> For example,
>>>> response.append("\"").append(value).append("\"");
>>>> will perform much better than,
>>>> response.append("\"" + value + "\"");
>>>>
>>>> With some of the enhancements in compilers and run time JIT
>>>> compilers this kind of transformation may get done automatically.
>>>> However, IMO, the explicit change made by hamada is a better approach.
>>>>
>>>> Nice job Hamada.
>>>>
>>>> Btw, I'm gonna ask Tom Ball who is the creator of Jackpot if a
>>>> Jackpot transformation rule could easily be created to identify
>>>> such a usage and also provide the transformation. That'd be a nice
>>>> transformation to have.
>>>>
>>>> charlie ...
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
>>>> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
>>> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
>> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe_at_grizzly.dev.java.net
> For additional commands, e-mail: dev-help_at_grizzly.dev.java.net
>
--
Charlie Hunt
Java Performance Engineer
630.285.7708 x47708 (Internal)
<http://java.sun.com/docs/performance/>