Optimize software performance

zhaozj2021-02-11  210

Method for optimizing software performance Liu Yanqing Yesky

Add smaller auxiliary functions

In the initial version of the Swing toolkit, create too many Point, Rectangle, and Dimension objects severely affect the performance of the program. Although multiple values ​​returns multiple values ​​in a Point or Rectangle object seem more efficient, the cost of this is much higher than that of the modifier. Before the recent SWING version is launched, this problem can be improved by simply adding some auxiliary methods in the component or other class, as shown below:

Public int getX () {return mybounds.x;} public int getH () {return mybounds.height;} public int getWidth;}

Now, the calling program can 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 () can still be used, smaller auxiliary functions only provide a higher efficiency of the same goal, and the result is that Rectangle's interface will be fully exposed to the components. When modifiable Swing makes it support and can use these smaller auxiliary functions, the result is twice as much as possible in Swing. Since the GUI code is sensitive to performance, this improvement is significant.

The negative effects of this technology are more methods of object owners, and to obtain the same information, there are many ways, which makes the files quite large and more complicated, which is not conducive to the use of this technology. . However, such optimization techniques are still very effective as Swing examples.

Variable use

In addition to adding a temporary function with a simple data type value in the component, Java 2 also uses other technologies to reduce activities in AWT and SWING. Add another version of GetBounds () to the components and other GUI classes, you can get the calling program to get the return value of 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 you can reuse it in future calls. If a caller repeatedly calls many Component objects, you can create a Rectangle object and use it in each Component. It should be noted that this technology only applies to variable object types, which cannot be reduced in this way to reduce String class objects.

Combined with two long

A better way to create a simple class such as Point is to make the Point class becomes unstrenomed, then define a variable subclass, and the specific method is shown in the example below:

Public class point {

Protected int x, y;

Public Point (int x, int y) {this.x = x; this.y = y;} public final int getX () {returnix 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 securely return to myLocation's address because the calling program returns an error message if you try to modify these domains or call their Regulator. Of course, the calling program can still convert Point to MutablePoint, but it is clear that this will bring unsafe, although the calling program will also get the return value they need. The developer of C will notice that this technology is a constant address that returns a Rectangle in C (const rectangle &) is a bit similar to Java does not have such features.

In the Java.math.Biginteger class in the Java 1.3 class, a class does not need to create a new object to return a "read-only" object. The MutableBiginteger class is not open, it is only used inside the Java.math class library. However, due to some of the Biginteger class (for example, GCD ()) is composed of many arithmetic operations, the performance of these operations without creating a temporary object will greatly improve the performance.

转载请注明原文地址:https://www.9cbs.com/read-4521.html

New Post(0)