aspose file tools*
The moose likes Performance and the fly likes Who can help me improve performance of these methods? thanks! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "Who can help me improve performance of these methods? thanks!" Watch "Who can help me improve performance of these methods? thanks!" New topic
Author

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

Javan Li
Ranch Hand

Joined: Jul 24, 2002
Posts: 84
<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

Joined: Jan 30, 2000
Posts: 18671
I'm moving this to Performance...


"I'm not back." - Bill Harding, Twister
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Javan Li
Ranch Hand

Joined: Jul 24, 2002
Posts: 84
But if the method was invoked 3862 times with every operation , Doesn't StringBuffer help here??
Thanks very much!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jul 24, 2002
Posts: 84
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jul 02, 2003
Posts: 5
static final String onlyCreatedOnce= "REP_PRO_";

String newString = onlyCreatedOnce + (new stuff)


Sun Certified Java Programmer 1.4
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jul 02, 2003
Posts: 5
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Jul 24, 2002
Posts: 84
i see , thanks very much!!
James Fisk
Greenhorn

Joined: Jul 02, 2003
Posts: 5
I stand corrected.
You are really on it!
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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()
!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Who can help me improve performance of these methods? thanks!
 
Similar Threads
HashMap in HashMap Doubts
Generics : defining their relationship in order to ovloard a method
Replace tokens in a file
maps of classes of enums
is object a list of string or only a string?