1 2 Previous Next 15 Replies Latest reply: Jan 3, 2011 1:35 AM by 827266 RSS

    Help on improving Performance

    827266
      I have three doubts in perofrmance fix

      1. Is better to use Switch case (using enums for string) or if else .. if the options are exceeding 50.
      2. There is a POJO class. am calling obj.getxxx() method several times in other class instead of creating a variable for it.
      Is it a better practice or creating a variable and using it is better?
      3. This is regarding JDBC and JSP using Oracle10g,Tomcat and JBOSS.Currently this is the scenario. I am retreiving more than 1000 records in resultset. But actually the user requires 10 records..rarely he requires 1000 records. Am iterating thru a loop in DAO class and getting that particular records in while(resultset,next()) and passing them to JSP (thru request obect)
      If the user again requests for 20 records..am again doing the same.
      Because of this the query is executing again and again. and the server is becoming very slow.
      (The code written long back and also not upto the standards and performance is not considered while developing)
      But Now, i want to change it like..
      1.Keepiing the resultset in a map and want to store in sessionObject. whenever the user again wants 10,20, 30 or all the records , i wil get them from session and wants to display in jsp.
      2.Using rowid concept in query itself while retreiving the records.
      Which one is better? or is there any otherways of doing it

      Pls help and correct me..
        • 1. Re: Help on improving Performance
          Kayaman
          user13522880 wrote:
          I have three doubts in perofrmance fix

          1. Is better to use Switch case (using enums for string) or if else .. if the options are exceeding 50.
          This has nothing to do with performance. If there are long sections of if-else clauses a redesign might be in order to make the code more readable and easier to modify/develop further.
          2. There is a POJO class. am calling obj.getxxx() method several times in other class instead of creating a variable for it.
          Is it a better practice or creating a variable and using it is better?
          Again, nothing to do with performance. The code is usually easier to follow if you use a local variable instead of using a getter all the time.
          1.Keepiing the resultset in a map and want to store in sessionObject. whenever the user again wants 10,20, 30 or all the records , i wil get them from session and wants to display in jsp.
          2.Using rowid concept in query itself while retreiving the records.
          Which one is better? or is there any otherways of doing it
          There is rarely a clearcut "this is better than that" answer to questions in programming. It all depends on the system as a whole, what the design is etc.
          If you store 1000 rows in the session and can afford that design and memorywise, then sure, why not. Using limit/rowid in queries works too. Caching might be an option too.
          • 2. Re: Help on improving Performance
            user575089
            Kayaman wrote:
            user13522880 wrote:
            I have three doubts in perofrmance fix

            1. Is better to use Switch case (using enums for string) or if else .. if the options are exceeding 50.
            This has nothing to do with performance.
            wrong.

            that depends on the compiler and the CPU. run a realtime test. executes the test case . Measure the time takes and make your own decision.
            • 3. Re: Help on improving Performance
              Kayaman
              user575089 wrote:
              wrong.
              that depends on the compiler and the CPU. run a realtime test. executes the test case . Measure the time takes and make your own decision.
              No, quite right. Of course you can create micro-benchmarks to show how a switch is infinitesimally faster than if-else in some random, unrelated situation.

              But I thought we were talking about performance here, not counting CPU cycles like some Junior level developer who just landed his first job out of school.
              • 4. Re: Help on improving Performance
                827266
                Hi Kayman

                I just read these below from Oreally

                "Avoid creating temporary objects within frequently called methods"
                "Avoid creating objects in frequently used routines. Because these routines are called frequently, you will likely be creating objects frequently, and consequently adding heavily to the overall burden of object cycling"

                these methods are called frequently, hence i think it would be better to use getmethod..
                The same thing will be used in JSTL kind of APIs while calling class fileds. For example in JSTL
                obj.getxxx willl be written like obj.xxx
                • 5. Re: Help on improving Performance
                  Kayaman
                  user13522880 wrote:
                  Hi Kayman

                  I just read these below from Oreally
                  Oh, really? Maybe you mean O'Reilly?
                  "Avoid creating temporary objects within frequently called methods"
                  "Avoid creating objects in frequently used routines. Because these routines are called frequently, you will likely be creating objects frequently, and consequently adding heavily to the overall burden of object cycling"
                  these methods are called frequently, hence i think it would be better to use getmethod..
                  Do you know the difference between creating an object and assigning a reference? It seems like you don't.
                  The same thing will be used in JSTL kind of APIs while calling class fileds. For example in JSTL
                  obj.getxxx willl be written like obj.xxx
                  And JSTL has nothing to do with your misunderstandings.
                  • 6. Re: Help on improving Performance
                    gimbal2
                    user13522880 wrote:
                    Hi Kayman

                    I just read these below from Oreally

                    "Avoid creating temporary objects within frequently called methods"
                    Seems like an old source; modern JVMs can perfectly cope with short lived objects.
                    "Avoid creating objects in frequently used routines. Because these routines are called frequently, you will likely be creating objects frequently, and consequently adding heavily to the overall burden of object cycling"
                    ... this says exactly the same. Again, nowadays it is basically something you should not be worrying about.

                    >
                    these methods are called frequently, hence i think it would be better to use getmethod..
                    The same thing will be used in JSTL kind of APIs while calling class fileds. For example in JSTL
                    obj.getxxx willl be written like obj.xxx
                    You think, you make unrelated comparisons (JSTL is not java code) and you quote others. Do you have anything solid of your own to add?


                    The deal is still the same today as it has been for a long time: the JVM is much better at optimizing than you are. Therefore it is pointless and counterproductive to worry about optimization until you have proof that you have a bottleneck somewhere. And when that happens you don't start to make guesses, educated or otherwise; you use a profiler to exactly tell you which part of the code is slow / using lots of resources. Only then you start to think about reasons why it is slow and how to improve performance.


                    So take kayaman's indirect advice and worry about readable and maintainable code, not about the speed of it.
                    • 7. Re: Help on improving Performance
                      827266
                      Thank u Kayman for clarifications

                      And regarding the third question,
                      The issue is with the queries and getting the results in jsp in repeated number of times
                      We are using several lists and maps which are hitting perfomance (identified using Jprofiler)
                      can u suggest any better way of doing it
                      • 8. Re: Help on improving Performance
                        gimbal2
                        user13522880 wrote:
                        We are using several lists and maps which are hitting perfomance (identified using Jprofiler)
                        Very good (the JProfiler part, not the lists and maps part).
                        can u suggest any better way of doing it
                        How can anybody suggest a better way of doing something when you give only a vague description of what you are doing right now? Post a code example that demonstrates what you are doing.
                        • 9. Re: Help on improving Performance
                          jschellSomeoneStoleMyAlias
                          user13522880 wrote:
                          can u suggest any better way of doing it
                          Performance in terms of business needs is impacted by the following.
                          1. Requirements - most impact.
                          2. Architecture/design
                          3. Implementation - least impact.

                          The last should be addressed by using a profiler in a situation where the system is running as close what will actually happen in production as possible.

                          If you have a database problem in terms of querying data (and only that) the most effective way to address it is to stop doing the queries. If you don't do the queries then it will be much faster. You do that by caching the data. You address that at step 2 in the above.
                          • 10. Re: Help on improving Performance
                            827266
                            I am about to do the caching technique. But, as far as my application is concerned we are advised not to cache large amount of data. Hence I needed help on this. Now I am considering fine tuning resultset or stored procedure concepts.

                            Thank you jschell, kayman for your guidance and
                            also everyone for your valuble suggestions.

                            Dear gimbal2
                            I needed advice on third point not on alternatives for lists and maps. i have not posted it clearly.
                            • 11. Re: Help on improving Performance
                              803795
                              You say the server is becoming slow. Are you using connection pooling to get your data? You can speed up your queries dramatically if you are doing so. (don't forget to get/use/close a connection in a try/catch/finally block. Don't hold onto connections such as for the duration of the session).

                              Normally, I would look at allowing users to fetch data by providing a filter to limit what he wants to fetch. One way to do so to provide a link to fetch the 'next 10 records', a better way is to provide a more intuitive filter such as "fetch all records from startDate through endDate', or if you have a phone book as a database,' fetch all records whose last name begins with X'. The nature of the filter depends on the nature of the data.

                              I assume your query fetches a set of records at once, rather than one record at a time which would be very slow.

                              Personally, I'm more concerned that a program is well structured and easy to read and understand, rather than low level optimizations have been done that provide little performance gain, but makes the code hard to understand.
                              • 12. Re: Help on improving Performance
                                827266
                                Hi njbt7y ..
                                The framework is wrapped up with EJB, I am using Connection pool and try catch block..
                                so i am concenrating on query levels..whcih are complex quiries and are taking 20 to 25 seconds of time for executing
                                even after finetuning by our DBA team.
                                I want to put my effort by optimizing at code level. Hencelooking at fetching the records.
                                Present code is fetching results at a time. Then in loop getting particular records by guiving startingIndex, EndingIndex and number of records.

                                I wanted to change it to get only particuar records using fetch() in resultset..
                                Please suggest..and by the way
                                Wish you a happy new year...
                                • 13. Re: Help on improving Performance
                                  803795
                                  Sorry, I'm out of ideas.
                                  • 14. Re: Help on improving Performance
                                    jschellSomeoneStoleMyAlias
                                    user13522880 wrote:
                                    Hi njbt7y ..
                                    The framework is wrapped up with EJB, I am using Connection pool and try catch block..
                                    so i am concenrating on query levels..whcih are complex quiries and are taking 20 to 25 seconds of time for executing
                                    even after finetuning by our DBA team.
                                    What does that mean?

                                    One idiom could be that you do a query for primary ids. Then you fetch each object using the primary id.

                                    Thus to get 15 items you do 16 queries.

                                    Obviously the way to speed that up would be to do one query and get the items on the first query.
                                    I want to put my effort by optimizing at code level. Hencelooking at fetching the records.
                                    Present code is fetching results at a time. Then in loop getting particular records by guiving startingIndex, EndingIndex and number of records.
                                    How many? What is the size of each record? Are there any blobs involved? What is the actual network infrastructure between the app box and the database.

                                    As an example retrieving 25 customer records, even if you were doing 26 queries in a normal data center set up should take less than 1 second.
                                    Pretty much regardless of what you are doing (the queries not post processing.)

                                    So if that isn't the case then something is wrong outside of the java code.

                                    Conversely where EXACTLY are you measuring the time it takes. Are you measuring the actual database calls or are you measuring something at the browser level?
                                    1 2 Previous Next