12 Replies Latest reply: Apr 27, 2007 12:41 AM by jwenting RSS

    JAVA performance issue

    807606
      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?
        • 1. Re: JAVA performance issue
          807606
          the first case might be just a little better
          • 2. Re: JAVA performance issue
            807606
            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
            • 3. Re: JAVA performance issue
              807606
              the first case might be just a little better
              it 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
              • 4. Re: JAVA performance issue
                807606
                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.
                • 5. Re: JAVA performance issue
                  807606
                  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.
                  • 6. Re: JAVA performance issue
                    807606
                    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
                    • 7. Re: JAVA performance issue
                      807606
                      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?
                      • 8. Re: JAVA performance issue
                        798701
                        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.
                        • 9. Re: JAVA performance issue
                          807606
                          Its my idea. Do you have any idea/ If yes, please share it.
                          • 10. Re: JAVA performance issue
                            807606
                            Issues of performance? This may be related to latter replies in this thread:

                            http://forum.java.sun.com/thread.jspa?threadID=5164040&start=45
                            • 11. Re: JAVA performance issue
                              807606
                              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 favourite articles 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
                              • 12. Re: JAVA performance issue
                                jwenting
                                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?