JAVA performance issue

I have two methods in a class. I have call those methods in two different way. The method's code are written below:

public String getCompany(String name)

{}

public String getCompanyDetails(String company)

{}

I want to call those methods in the following two way:

Case 1

public static void main(String arg[])

{

String name = "Employee";

String addDetails = getCompanyDetails(getCompany(name));

}

Case 2

public static void main(String arg[])

{

String name = "Employee";

String company = getCompany(name);

String addDetails = getCompanyDetails(company );

}

Which one is better approach? Which one will give me better performance?

[762 byte] By [@RIGOa] at [2007-11-27 2:27:57]
# 1
the first case might be just a little better
calvino_inda at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 2
how do you know you have a performance issue? clue: thanks to modern JVMs and their optimizing qualities, you almost certainly don't. never just assume something is going to be a performance bottleneck, do some testing to prove it
georgemca at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 3
> the first case might be just a little betterit almost certainly won't be, and in any case, if it were, we're talking about a few clock cycles - who's going to notice that, in an application that's accessing all sorts of other, slower resources? nobody, that's who
georgemca at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 4
> Which one is better approach? Which one will give me> better performance?You will get no noticeable performance improvement in one case vs the other.
karma-9a at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 5
All I can do is agree with the above posters. The extremely small time it will take to create the space needed for company and then assign a value to that is negligible.
jjhusa01a at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 6
> All I can do is agree with the above posters. The> extremely small time it will take to create the space> needed for company and then assign a value to that is> negligible.plus it's likely the compiler will inline the getCompany method anyway
georgemca at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 7
Guys, this is just a example. In my case, the called function will call another function. To execute all the line of code it will hardly taking a minute. But, i want to know the beeter approach. I think 2nd one is the better approach. What do u think?
@RIGOa at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 8

> Guys, this is just a example.

So you don't actually have a performance issue at all?

> I think 2nd one is the better approach.

Do you have anything to back your claim?

> What do u think?

I think that you need empirical evidence to find out if X is 'better' than Y performance-wise.

jsalonena at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 9
Its my idea. Do you have any idea/ If yes, please share it.
@RIGOa at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 10
Issues of performance? This may be related to latter replies in this thread: http://forum.java.sun.com/thread.jspa?threadID=5164040&start=45
DrLaszloJamfa at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 11

> Its my idea. Do you have any idea/ If yes, please

> share it.

you've been given a load of ideas above. the biggest idea, though, is that you shouldn't ever make any assumptions about what your performance bottlenecks will be. allow me to trot out one of my [url=http://java.sun.com/developer/technicalArticles/Interviews/goetz_qa.html]favourite articles[/url] once more :-)

regarding whether it is ok for you to embed a call to one method inside another a la

getCompanyName(getOtherData());

that's perfectly acceptable. if the methods are small enough - and they ought to be - the JVM will inline them anyway, that is, at runtime the two methods will be compiled into one larger, faster method. another consideration, though, is "what if the getOtherData() method returns null, or some other unsuitable value?". do you need special handling for that in the getCompanyName method, or in the method from which these calls are being made? that's the sort of question you need to ask yourself in these situations, not the "performance" red herring. in a system that paints a GUI, performs disk I/O or other resource-intensive work, trying to save a couple of clock cycles like above is a complete waste of time

georgemca at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...
# 12

and he thinks he's got performance problems...

Wonder what he'll do when he's got to deal with code that executes in milliseconds but spends minutes waiting for databases and network connections.

Will he still wonder whether he could shave off a few clock cycles by using obscure or unmaintainable code constructs?

jwentinga at 2007-7-12 2:39:20 > top of Java-index,Java Essentials,Java Programming...