A DESCRIPTION OF THE REQUEST :
Javac does not fully optimize the code when the concatenation with "+" is used.
JUSTIFICATION :
performance impoved by about 20% on the code provided below
(comparison between the 2 methods 'loopBasic' and 'loopStringBuilder')
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
//method provided below decompiled with jad SHOULD BE
private String loopBasic(String s) {
String s1 = new String();
for(int i = 0; i < 0x989680; i++)
s1 = (new StringBuilder("A")).append(s).append("C").toString();
return s1.toString();
}
ACTUAL -
//method provided below decompiled with jad
private String loopBasic(String s) {
String s1 = new String();
for(int i = 0; i < 0x989680; i++)
s1 = (new StringBuilder()).append("A").append(s).append("C").toString();
return s1.toString();
}
---------- BEGIN SOURCE ----------
//To compile, execute to get the performance result, to decompile with jad
public class StringBufferTest{
private String loopBasic(String aString) {
String essai = new String();
for (int i =0 ; i<10000000 ; i++) {
essai = "A" + aString + "C";
}
return essai.toString();
}
private String loopStringBuffer(String aString) {
StringBuffer essai = new StringBuffer();
for (int i =0 ; i<10000000 ; i++) {
essai = new StringBuffer("A").append(aString).append("C");
}
return essai.toString();
}
private String loopStringBuilder(String aString) {
StringBuilder essai = new StringBuilder();
for (int i =0 ; i<10000000 ; i++) {
essai = new StringBuilder("A").append(aString).append("C");
}
return essai.toString();
}
public static void main(String args[]){
StringBufferTest stringBufferTest = new StringBufferTest();
System.out.println("Start loopBasic : " + System.currentTimeMillis());
System.out.println(stringBufferTest.loopBasic("string"));
System.out.println("start loopStringBuffer : " + System.currentTimeMillis());
System.out.println(stringBufferTest.loopStringBuffer("string"));
System.out.println("Start loopStringBuilder : " + System.currentTimeMillis());
System.out.println(stringBufferTest.loopStringBuffer("string"));
System.out.println("End : " + System.currentTimeMillis());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
obviously, use StringBuilder instead
Javac does not fully optimize the code when the concatenation with "+" is used.
JUSTIFICATION :
performance impoved by about 20% on the code provided below
(comparison between the 2 methods 'loopBasic' and 'loopStringBuilder')
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
//method provided below decompiled with jad SHOULD BE
private String loopBasic(String s) {
String s1 = new String();
for(int i = 0; i < 0x989680; i++)
s1 = (new StringBuilder("A")).append(s).append("C").toString();
return s1.toString();
}
ACTUAL -
//method provided below decompiled with jad
private String loopBasic(String s) {
String s1 = new String();
for(int i = 0; i < 0x989680; i++)
s1 = (new StringBuilder()).append("A").append(s).append("C").toString();
return s1.toString();
}
---------- BEGIN SOURCE ----------
//To compile, execute to get the performance result, to decompile with jad
public class StringBufferTest{
private String loopBasic(String aString) {
String essai = new String();
for (int i =0 ; i<10000000 ; i++) {
essai = "A" + aString + "C";
}
return essai.toString();
}
private String loopStringBuffer(String aString) {
StringBuffer essai = new StringBuffer();
for (int i =0 ; i<10000000 ; i++) {
essai = new StringBuffer("A").append(aString).append("C");
}
return essai.toString();
}
private String loopStringBuilder(String aString) {
StringBuilder essai = new StringBuilder();
for (int i =0 ; i<10000000 ; i++) {
essai = new StringBuilder("A").append(aString).append("C");
}
return essai.toString();
}
public static void main(String args[]){
StringBufferTest stringBufferTest = new StringBufferTest();
System.out.println("Start loopBasic : " + System.currentTimeMillis());
System.out.println(stringBufferTest.loopBasic("string"));
System.out.println("start loopStringBuffer : " + System.currentTimeMillis());
System.out.println(stringBufferTest.loopStringBuffer("string"));
System.out.println("Start loopStringBuilder : " + System.currentTimeMillis());
System.out.println(stringBufferTest.loopStringBuffer("string"));
System.out.println("End : " + System.currentTimeMillis());
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
obviously, use StringBuilder instead
- duplicates
-
JDK-4059189 Compiler generated code for String.operator+() could avoid allocations.
-
- Closed
-