• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Who can help me improve performance of these methods? thanks!

 
Javan Li
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<1> Code:
public static String getAliasName(ExtensionInfoRec extInfo)
throws java.lang.Exception{
StringBuffer strAliasName=new StringBuffer();
if (extInfo.getExtensionBelong() ==
ExtensionInfoTable.EXTENSION_BELONG_REPORT_PRODUCT) {
strAliasName.append("REP_PRO").append("_").append(extInfo.getColName());
}
else {
strAliasName.append(extInfo.getTableName()).append("_").append(extInfo.getColName());
}
return strAliasName.toString();
}
<2> Code:
private Map mapRecItem_ = new HashMap(100);
private Map mapRecItemForCode_ = new HashMap(100);
...
...
protected void set(String strTableName,
String strColName,
CustomizeRecItem item) {
StringBuffer sb=new StringBuffer(25);
if (item != null) {
mapRecItem_.put(sb.append(strTableName).append(":").append(strColName).toString().toUpperCase(), item);
int extCode = item.getExtensionInfo().getExtensionCode();
mapRecItemForCode_.put(new Integer(extCode),item);
}
}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm moving this to Performance...
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only way these methods could be slow is by the getter methods of ExtensionInfoRec or CustomizeRecItem being slow.
BTW, using StringBuffer doesn't buy you anything here. Using String would be as fast and improve readability.
Why do you think you need to improve performance of these methods?
 
Javan Li
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But if the method was invoked 3862 times with every operation , Doesn't StringBuffer help here??
Thanks very much!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Javan Li:
But if the method was invoked 3862 times with every operation , Doesn't StringBuffer help here??
Thanks very much!

No, it doesn't. You would be creating 3862 StringBuffers anyway - why should it help?
Take a look at http://www.coderanch.com/t/201940/Performance/java/Clueless-drives-Performance
Perhaps we should put this in a FAQ...
 
Javan Li
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
strAliasName.append("REP_PRO").append("_").append(extInfo.getColName());
But use String instead of above ,
doesn't it create 3862*3 Strings here ??
[ July 02, 2003: Message edited by: Javan Li ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No - the typical Java compiler uses a single StringBuffer for all String concatenations in an expression.
For example
"REP_PRO" + "_" + extInfo.getColName()
would be *identical* to
new StringBuffer().append("REP_PRO").append("_").append(String.valueOf(extInfo.getColName)).toString()
 
James Fisk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
static final String onlyCreatedOnce= "REP_PRO_";

String newString = onlyCreatedOnce + (new stuff)
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by James Fisk:
static final String onlyCreatedOnce= "REP_PRO_";

String newString = onlyCreatedOnce + (new stuff)

Doesn't buy you anything, too.
As stated by the Java Language Specification, String literals are put into a pool and reused throughout the application.
Try
System.out.println("myString" == "myString");
You should get "true" as output, as both literals are resolved to the *same* String object in the constant pool. There is no instanciation overhead involved at all!
 
James Fisk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was referring to having only one concatenation
instead of TWO

I know about the String pool.. duh... JAVA 101
each concatenation creates a NEW STRING
he had TWO concatenation operators in his expression.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
he had TWO concatenation operators in his expression.
each concatenation creates a NEW STRING

Nope. The JLS allows an implementation to be smart enough to not create an intermediate String if there's no real need. And any decent compiler will take advantage of this permission. If a concatenation is followed immediately by another concatenation, the compiler knows that there's no use for the intermediate String, and so doesn't create one. If the program logic had been a bit more complex, then there might have been a benefit to using StringBuffer rather than + concatenation - but as it is, there is no benefit.
 
Javan Li
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see , thanks very much!!
 
James Fisk
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I stand corrected.
You are really on it!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Additionally, "REP_PRO" + "_" is a Compile Time Constant Expression and therefore evaluated at compile time, not at runtime - I missed that in my first post...
So
"REP_PRO" + "_" + extInfo.getColName()
would in fact be identical to
new StringBuffer().append("REP_PRO_").append(String.valueOf(extInfo.getColName)).toString()
!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic