JAVA Performance Optimization

Source: Internet
Author: User
Tags gety

 


Add a smaller helper Function

In the original version of the Swing toolkit, creating too many Point, Rectangle, and Dimension objects seriously affects program performance. Although it seems more efficient to return multiple values in a Point or Rectangle object at a time, this is much more costly than calling multiple methods. Before the latest Swing version is released, this problem can be improved by simply adding helper methods to components or other classes, as shown below:

Public int getX () {return myBounds. x ;}
Public int getY () {return myBounds. y ;}
Public int getHeight () {return myBounds. height ;}
Public int getWidth () {return myBounds. width ;}


Now, the caller can get the same result without creating a temporary object, as shown below:

Int x = component. getX ();
Int y = component. getY ();
Int h = component. getHeight ();
Int w = component. getWidth ();


The original getBounds () can still be used. A smaller helper function only provides a way to achieve the same goal more efficiently. The result is, the Rectangle interface is completely exposed to the component interface. After modifying Swing to support and use these smaller auxiliary functions, the result is that many operations in Swing run twice faster. This improvement is significant because the GUI code is sensitive to performance.

The negative effect of this technology is that the object has more methods, and there are multiple methods to obtain the same information, which makes the file quite large and more complex, this technology is not conducive to users. However, as shown in the Swing example, this optimization technology is very effective in the case of high performance requirements.


Utilization of variability

In addition to adding temporary functions with simple data type values, such as getX (), as discussed above, java 2 also uses other technologies to reduce object creation activities in AWT and Swing. Add another version of getBounds () to the component and other GUI classes, so that the caller can get a return value similar to the Rectangle type without creating a temporary object:

Public Rectangle getBounds (Rectangle returnVal ){

ReturnVal. x = myBounds. x;

ReturnVal. y = myBounds. y;

ReturnVal. height = myBounds. height;

ReturnVal. width = myBounds. width;

Return returnVal;

}

The caller must still create a Rectangle object. However, you can reuse it in future calls. If a caller calls Multiple Component Objects repeatedly, you can create a Rectangle object and use it in each Component. Note that this technique is only applicable to variable object types. It is not possible to reduce the number of String class objects created in this way.

Combine the strengths of the two companies

A better way to solve the problem of creating objects in simple classes such as Point is to make the Point class immutable and then define a variable subclass. The specific method is as follows:

Public class Point {

Protected int x, y;

Public Point (int x, int y) {this. x = x; this. y = y ;}

Public final int getX () {return x ;}

Public final int getY () {return y ;}

}

Public class MutablePoint extends Point {

Public final void setX (int x) {this. x = x ;}

Public final void setY (int y) {this. y = y ;}

}

Public class Shape {

Private MutablePoint myLocation;

Public Shape (int x, int y) {myLocation = new MutablePoint (x, y );}

Public Point getLocation () {return (Point) myLocation ;}

}

In the above example, Shape can safely return the myLocation address, because if the caller attempts to modify these domains or calls their "regulator", an error message will be returned. Of course, the caller can still convert the Point to MutablePoint, but it is obvious that this will lead to insecure, although the caller will also get the returned values they need .) C ++ developers will note that this technology is a bit similar to const Rectangle () that returns a Rectangle in C ++, and does not have such features.

In the Java. math. BigInteger class in the java 1.3 class library, a "read-only" object is returned if a class does not need to create a new object. The MutableBigInteger class is not public and is only used within the java. math class library. However, some methods (such as gcd () in the BigInteger class are composed of many arithmetic operations. To complete these operations without creating temporary objects, the performance of the program is greatly improved.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.