Forums Register Login

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

+Pie Number of slices to send: Send
<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);
}
}
+Pie Number of slices to send: Send
I'm moving this to Performance...
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
But if the method was invoked 3862 times with every operation , Doesn't StringBuffer help here??
Thanks very much!
+Pie Number of slices to send: Send
 

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 https://coderanch.com/t/201940/Performance/java/Clueless-drives-Performance
Perhaps we should put this in a FAQ...
+Pie Number of slices to send: Send
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 ]
+Pie Number of slices to send: Send
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()
+Pie Number of slices to send: Send
static final String onlyCreatedOnce= "REP_PRO_";

String newString = onlyCreatedOnce + (new stuff)
+Pie Number of slices to send: Send
 

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!
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
i see , thanks very much!!
+Pie Number of slices to send: Send
I stand corrected.
You are really on it!
+Pie Number of slices to send: Send
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()
!!!
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1364 times.
Similar Threads
is object a list of string or only a string?
Generics : defining their relationship in order to ovloard a method
HashMap in HashMap Doubts
Replace tokens in a file
maps of classes of enums
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:05:06.