Several ways to optimize Java performance

Source: Internet
Author: User
Tags final gety

To add a smaller auxiliary function

In the initial version of the Swing toolkit, creating too many point, rectangle, and dimension objects can seriously affect program performance. Although it seems more efficient to return multiple values in one point or Rectangle object at a time, this is much more expensive than calling multiple methods. This issue can be improved by simply adding some auxiliary methods to components or other classes before the latest swing version is introduced, as follows:

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

The caller can now get the same result without creating a temporary object, as follows:

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

The original getbounds () is still available, and a smaller auxiliary function simply provides a more efficient way to achieve the same goal, and the result is that the rectangle interface is completely exposed to the component's interface. When you modify swing to support and use these smaller accessible functions, the result is that many of the operations in swing are running twice times faster than they used to. Because GUI code is more sensitive to performance, this improvement is important.

The negative effect of this technique is that the object has more methods, and there are many ways to get the same information, which makes the file quite large and more complex, which is not conducive to user adoption of this technology. However, as the swing example shows, this optimization technique is very effective in situations where performance requirements are high.

The use of variability

In addition to adding temporary functions with simple data type values, such as the Getx () discussed above, in the component, Java 2 uses other techniques to reduce object creation activities in AWT and swing. Adding a different version of GetBounds () to the component and other GUI classes allows the caller to get a return value like 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 calling program still has to create a rectangle object, but it can be reused in later invocations. If a calling program repeatedly calls many component objects, you can create a rectangle object and use it in each component. It should be noted that this technique applies only to mutable object types, and it is not possible to reduce the creation of a string class object in this way.

Combined with the length of two families

A better way to solve the problem of object creation for simple classes such as point is to make the points class immutable and then define a mutable subclass, as in the following example:

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 address of the mylocation, because the caller will return an error message if it attempts to modify these domains or call their "adjuster". Of course, the caller can still convert point to Mutablepoint, but it's obviously unsafe, although the caller will get the return value they need. C + + developers will notice that this technique is somewhat similar to the constant address (const rectangle&) of returning a Rectangle in C + + ━━java does not have such an attribute.

In the Java.math.BigInteger class in the Java 1.3 Class library, a class returns a "read-only" object without creating a new object. The Mutablebiginteger class is not exposed and is intended for use only within the Java.math class library. However, because some of the methods in the BigInteger class (such as GCD ()) are composed of many arithmetic operations, completing these operations without creating a temporary object greatly improves the performance of the program.

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.