Wanted: Efficient String Escape Method
Regarding the comparison in my last email....
Sorry, by mistake, I forgot to test the solution presented by Mark Gibson.
And, the "other" refers to Paul's combination of all suggestions.
Here are the results:
On Thu, 16 Mar 2000, Silvio Macedo wrote:
> String with no \ or " String with \ and "
> My proposal 26 42
> The "other" 34 288
Mark's 33 97
The difference probably comes from the repeated allocation of memory in
Mark's solution. So, the conclusions, derived from all people
involved, are, by order of impact:
- Avoid creation of a new String if the original String doesn't need to be
escaped.
- Avoid allocation of any new object inside (namely the StringBuffer)
- Avoid calling methods from String or StringBuffer (new objects in stack);
- Avoid reallocation of working buffer each time the method is called;
(Please note that in case there is a static array, the class is not
reentrant - I mean, in case of multithreaded use, it won't work.)
As a result, and for sake of robustness, it seems to me that Mark's
solution is the best (if, and only if, multiple threads will use this
class). Otherwise, my proposal is faster (significantly-more than 50%!).
Code to detect null string and zero length strings should also be
included in Mark's code to make it more robust and even more efficient.
It's fun to see that the old C approach is still better when speed is at
stake - even when doing it in Java 8-) .
Cheers, Silvio
``````````` Silvio Emanuel Nunes Barbosa de Macedo (PhD Std) '''''''''''''
[EMAIL PROTECTED] [EMAIL PROTECTED]
Intelligent and Interactive Systems Telecom. and Multimedia
Imperial College, University of London INESC Porto
Exhibition Road, Pc da Republica, 93
London SW7 2AZ, England 4050-497 Porto Portugal
Tel:+44 171 5946323 Tel:+351 22 2094220