Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Optimize Java 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
I've got many question about Java internal working to optimize my servlet. One of them is :

When i creat a new instance of an object, does java creat an instance for each upper-class of this object. So is it faster to creat an object inherited from Object than creating a sixth or seven level object.

Is there something in java "like" #Define that is not in the object stack ???

PS : sorry for my english....
 
When you create an instance of an object it does effectively create an instance of every class from which it inherents. This makes it a little slower to instantiate than simply instantiating one large class.

However, do not completely avoid inheritence because of speed. Correctness is primary, and you or someone else will probably have to look at this code again so make it maintainable. Fiddling with your inheritence structure is usually a lot of work, makes the whole project harder to understand and generally doesnt offer that big a speed enhancement.
 
I have also heard but have not fully tested the final capability on a class. The java runtime programs are constantly looking for the possibilites inheritance. if in your base class you declare it final, it should speed up your performance, possibley :)

later ackka
ackka@mad.scientist.com
duke_wave.gif
Java is the Future
 
If u r concerned about the speed with which your app runs, and you're using java, your concerns will never be addressed!
 
Java can perform at speeds that should be adequate for almost any application. I have written several Java applets and applications in Java for which execution time was a major concern and was always able to get sufficient speed out of Java.

With careful optimization your performance needs can be met. A couple of tips for anyone who is interested:

Declare variables and classes as final whenever you can. This lets the compiler optimise your code somewhat.

The main area in which Java is slow is in object creation and anything memory related. Minimize your memory usage and get as much memory grabbing done when your program starts as possible ie initialize as much as possible as the program starts. For a conventional application a delay of a few seconds while it starts is reasonable while a delay of a few seconds after that point can be intolerable. This is not terribly applicable to servlets as all the client sees is the net execution time so just settle for minimizing the number of objects created.

If your are frequently using data loaded from file or a database etc load it into a variable and use that instead.

Remember that alterations to individual instructions or other small scale alterations can take a few percent off execution time, while changes in the basic algorithm or data structure can cut execution time to a fraction of its former self.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top