Hey,
On Tue, 2007-01-09 at 18:01 -0800, David Daney wrote:
> Roman Kennke wrote:
> > Hi,
> >
> >
> >> This fixes a minor bug in Permission's toString method. I also changed
> >> it to use a StringBuffer instead.
> >>
> >
> > Why not use StringBuilder? Seems slightly more efficient.
> >
> > /Roman
> >
> >
> Why not leave it much as it was using the string concatenation operator
> '+'? The compiler converts this to String[Buffer|Builder] when it
> generates the byte code. IMO this patch makes the code much more
> difficult to understand with *no* improvement in efficiency.
>
I've written a quick benchmark that shows the difference between using
string concatenation, StringBuffer and StringBuilder. The test I used
is attached. Here are the results (in milliseconds) I got (more or
less):
Implementation Test 1 Test 2 Test 3 Test 4
string concatenation 9 120 950 8000
StringBuffer 8 130 750 7000
StringBuilder 7 130 740 7500
Here is the code for the different implementations I used:
String Concatenation:
String actions = "";
if (!getActions().equals(""))
actions = ' ' + getActions() + ' ';
String string = '(' + getClass().getName() + ' '
+ actions + getName() + ')';
return string;
StringBuffer:
String actions = "";
if (! getActions().equals(""))
actions = ' ' + getActions() + ' ';
StringBuffer string = new StringBuffer();
string.append('(').append(getClass().getName()).append(' ').
append(actions).append(getName()).append(')');
return string.toString();
StringBuilder:
String actions = "";
if (! getActions().equals(""))
actions = ' ' + getActions() + ' ';
StringBuilder string = new StringBuilder();
string.append('(').append(getClass().getName()).append(' ').
append(actions).append(getName()).append(')');
return string.toString();
Let me know your opinions/thoughts on this.
Cheers,
Tania
import java.awt.AWTPermission;
public class Test {
public static void main(String[]args)
{
AWTPermission permission = new AWTPermission("String");
///////////////////////////////////////////////////////////////////////////
long start = System.currentTimeMillis();
for (int i = 1; i <= 1000; i++)
permission.toString();
long end = System.currentTimeMillis();
long total = end - start;
System.out.println("Calling toString 1000 times: " + total);
///////////////////////////////////////////////////////////////////////////
start = System.currentTimeMillis();
for (int i = 1; i <= 10000; i++)
permission.toString();
end = System.currentTimeMillis();
total = end - start;
System.out.println("Calling toString 10000 times: " + total);
///////////////////////////////////////////////////////////////////////////
start = System.currentTimeMillis();
for (int i = 1; i <= 100000; i++)
permission.toString();
end = System.currentTimeMillis();
total = end - start;
System.out.println("Calling toString 100000 times: " + total);
///////////////////////////////////////////////////////////////////////////
start = System.currentTimeMillis();
for (int i = 1; i <= 1000000; i++)
permission.toString();
end = System.currentTimeMillis();
total = end - start;
System.out.println("Calling toString 1000000 times: " + total);
}
}