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]

> 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
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?
> 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.
> 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
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?