aspose file tools*
The moose likes Performance and the fly likes Help on improving Performance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "Help on improving Performance " Watch "Help on improving Performance " New topic
Author

Help on improving Performance

g venkata
Greenhorn

Joined: Dec 29, 2010
Posts: 5
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

Please help and correct me..



William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
1. I go with switch/case for all but the simplest branching but mainly for clarity and because I tend to think in terms of state machines.
2. Best practice is to use whatever is clearest - personally I use public variables unless the JavaBeans API is involved
3. I have no idea because I don't do JDBC but I bet you can get suggestions here.....

Bill
g venkata
Greenhorn

Joined: Dec 29, 2010
Posts: 5
2. Best practice is to use whatever is clearest - personally I use public variables unless the JavaBeans API is involved


Thank you bill for your suggestions..
But, i am using bean type setter and getter methods. like JavaBeans API
Shall I create a String variable for it.. and use it whereever requried?.
If so, I need to create 20 string variables like that.
or shall I call getter methods for all of them.
which would be better?

Please suggest
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3220
    
  49

William Brogden wrote:2. Best practice is to use whatever is clearest - personally I use public variables unless the JavaBeans API is involved

Couldn't this be a problem whenever you need to add some validation or other stuffs in future development? If setters and getters are used, such integration could be done without modifying the other components...
I agree that public exposure is suitable for final static variables, though.


Author of ExamLab ExamLab - a free SCJP / OCPJP exam simulator
What would SCJP exam questions look like? -- Home -- Twitter -- How to Ask a Question
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12825
    
    5
Here is what I have done when it is necessary to recognize Strings and switch accordingly.

Create a static String[] of all the Strings you need to recognize.
Create a static int[] matching the strings with integer values you will use in a big switch/case construct.
On creation of a class instance, build a Hashmap with the Strings as keys and the matching int values as Integer values.

At the point where you need to select code based on the value of a String, use it to look up the matching Integer and use that value in the switch.

Advantages: multiple words can be mapped to the same Integer, adding a new word/state is easy and does not change the rest of the code.

Bill
Ronald Djunaedi
Greenhorn

Joined: Jan 06, 2011
Posts: 5

g venkata wrote:I have three doubts in perofrmance fix
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..


for this thing, i have suggestion for you, get the records only in the exact what user needs, if they are needing 100 records, so only get the 100 records in the database level, you can use LIMIT for mysql, Rownum for ORACLE and so on.

If you only need 100 records, why would you fetch 1000 records ? the rest 900 will be useless and big enemy in performance.

Tejas Shah
Greenhorn

Joined: Oct 26, 2009
Posts: 23

Choosing on how much records to be fetched depends on the scenario.

1. If you need the records too frequently, you should not query the database again and again as connection to it and querying takes a lot of time, but keep it in memory(collection or some data structure) and return by iteration to the JSP. have more efficient query to the database.

2. If you don't need the records too frequently, you can query the database for exactly what you need even if it is done little frequently.


-Tejas
Cherish your life...!!
 
jQuery in Action, 2nd edition
 
subject: Help on improving Performance