Java Performance (Source: Java Programming Thoughts)

xiaoxiao2021-03-05  21

"This appendix is ​​submitted by Joe Sharp, and Java language emphasizes accuracy, but reliable behavior is considered for performance. This feature is reflected in automatic collection of garbage, strict period test, complete bytecode check, and conservative operation Aspects of synchronization, etc. For an interpreted virtual machine, since there are currently a large number of platforms available, it has further hindered the performance of performance. "First finish it, then gradually improve. Fortunately, it is usually not too much place where it needs to be improved. "(Steve McConnell" About Performance "[16]) This appendix is ​​to guide everyone to find and optimize" that is perfect ".

D.1 Basic method only After the program is properly and completely detected, the performance of performance: (1) Detecting the performance of the program in a realistic environment. If you meet the requirements, the target is achieved. If you do not meet, go to the next step. (2) Find the most deadly performance bottleneck. This may ask a certain skill, but all efforts will not be in a white fee. If you simplect the bottleneck, and try to optimize, it may be white flowers. (3) Use the speed of speed introduced in this appendix and return to step 1.

In order to make efforts not to be white, the positioning of the bottleneck is a crucial ring. Donald Knuth [9] has improved a program that spends 50% of the time to about 4% of the code. In just a working hour, he modified several lines of code to make the execution speed of the program. At this point, if the time continues to enter the residual code, it will only be lost. Knuth has a famous saying in the programming industry: "Premature Optimization Is The Root of All Evil). The most sensible approach is to suppress the impulse of premature optimization, because it can be missed a variety of useful programming technology, resulting in more difficult to understand and control, and more energy is maintained.

D.2 Looking for bottlenecks to find bottlenecks that most influencing program performance can take the following methods:

D.2.1 Inserting your test code inserts the "explicit" timing code to evaluate the program:

Long Start = system.currenttimemillis (); // The calculation code to be timed is LONG TIME = System.currentTimeMillis () - START;

Using System.out.Println (), a non-commonly used method prints the accumulated time to the console window. Because of an error, the compiler will ignore it, so you can use a "static final Boolean" (Static Final Boolean) to turn the code, so that the code can stay in the final release program, so you can take emergency . Although more complex evaluation means can also be selected, this is undoubtedly the easiest way for execution time for a measure of a specific task. System.currentTimeMillis () returned to one third of the second (1 millisecond). However, some systems have a time accuracy of less than 1 milliseconds (such as Windows PC), so it is necessary to repeat N times, and then divide the total time divided by N to obtain accurate time.

D.2.2 JDK Performance Evaluation [2] JDK supporting provides a built-in evaluation program that tracks the time spent on each routine and writes the evaluation result into a file. Unfortunately, the JDK evaluator is not stable. It works normally in JDK 1.1.1, but it is very unstable in subsequent versions. To run the evaluation program, add -PROF options when calling the unopened version of the Java interpreter. For example: java_g -prof myclass or plus a program (Applet): java_g -prof sun.applet.appletViewer Applet.html is not easy to understand the output information of the evaluation program. In fact, in JDK 1.0, it actually cuts the method named 30 characters. Therefore, some methods may not be distinguished. However, if you use the platform to support the -prof option, you can try the "HyperPorf" [3] or Greg White's "ProfileViewer" to explain the results. D.2.3 Special Tools If you want to keep up with the trend of performance optimization tools, the best way is to make some web sites. For example, "Tools for Optimizing Java" (Java Optimization Tools) made by Jonathan Hardwick: http://www.cs.cmu.edu/~jch/java/tools.html

D.2.4 Skills of Performance Evaluation ■ Due to the system clock due to evaluation, do not run any other processes or applications at that time to avoid affecting the test results. ■ If you have modified your own program, try (at least on the development platform) to improve its performance, then test the execution time of the code before and after the modification. ■ Try to perform each time test in a fully consistent environment. ■ If possible, you should design a test that does not rely on any user input, and avoids the result of the error.

D.3 Speed ​​Methods Now, the key performance bottleneck should have been isolated. Next, two types of optimizations can be applied: routine means and dependent on Java languages.

D.3.1 General means typically, a valid speed method is to redefine the program in a more realistic way. For example, in "programing pearls" [14], Bentley takes advantage of a novel data description, it can generate a very fast, and very streamlined inspector, introduce Doug Mcilroy to English language Expression. In addition, better algorithms may bring greater performance improvement than other methods - especially when the size of the data set is getting bigger and bigger. For details of these routine means, please refer to the "General Book" list at the end of this appendix.

D.3.2 Dependency Language Method For objective analysis, it is best to clarify the implementation time of various operations. In this way, the resulting result can be independent of the currently used computer - the final result is "standard time" by dividing the time to assign a value in the local area.

Operation example standard time

Local assignment i = n; 1.0 instance assignment this.i = n; 1.2int value added i ; 1.5byte value added B ; 2.0Short value added S ; 2.0double value value F ; 2.0double value value D ; 2.0 empty cycle while (true) n ; 2.0 Trimer expression (x <0)? -X: x 2.2 arithmetic call math.abs (x); 2.5 array assignment a [0] = n; 2.7long value-added L ; 3.5 method call funct (); 5.9throw or catch Abnormal try {throw e;} or catch (e) {} 320 synchronization method calls synchmehod (); 570 new object new object (); 980 new array new int [10]; 3100 passed its own system (such as my Pentium 200) Pro, Netscape 3 and JDK 1.1.5), these relative time unveiled: New objects and arrays will cause the most heavy overhead, synchronization will cause more heavy overhead, and one-time method call will cause moderate overhead . Reference Resources [5] and [6] summarizes the web address of the measuring program, you can run them on your own machine.

1. General Modification The following is some of the regular operational recommendations that speeds up the Java program key section (note the test results before and after the modification).

Will modify ...

Interface Abstract Class (Multiple Inherions for Interfaces) The Multiple Inherions of Interfaces The Optimized Non-Local or Arrse Circulatory Variable Variable Variable Variables Depending on the time-consuming time of the previous table, the time of the instance integer assignment is 1.2 for local integers. 1.2 Femback, but the array assignment time is a 2.7-fold link list (fixed size) saved by local integer assignment, or replaces the list to a loop array (generally known size) Each object is applied, which is equivalent to local assignment 980 Time. Refer to "Reuse Object" (next section), VAN WYK [12] P.87 and Bentley [15] P.81X / 2 (or 2 of any power) X >> 2 (or 2) Using faster hardware instructions

D.3.3 Special circumstances ■ Strings overhead: String Connection Operator seems simple, but actually consumes a lot of system resources. The compiler can be efficiently connected to the string, but the variable strings require a considerable processor time. For example, suppose S and T are string variables: system.out.println ("Heading" S "Trailer" T); the above statement requires new StringBuffer, string buffer), adds arguments, then use toString ) Convert the result back to a string. Therefore, regardless of disk space or processor time, it will be severely consumed. If you are ready to add multiple strings, consider using a string buffer directly to use a string buffer, especially when you reuse it in a loop. By disable new string buffering in each cycle, save 980 units of object creation time (as mentioned above). The performance can be further improved by using subString () and other string methods. If it is feasible, the speed of the character array can even be faster. It is also necessary to pay attention to the relationship of synchronization, so StringTokenizer will cause greater overhead. ■ Synchronization: In the JDK interpreter, call synchronization methods typically be more slower than that of the call disagreement method. After the JIT compiler is processed, the gap in this performance is raised to 50 to 100 times (note that the previous table summary shows a slow 97 times). So avoid using synchronous methods as much as possible - if you don't avoid it, the synchronization of the method is slightly more faster than the synchronization of the code block. ■ Reuse using objects: Time to get a long time to create a new object (depending on the time summary, the new time of the object is 980 times the assignment time, and the time to create a decimal set is 3100 times). Therefore, the most sensible approach is to save and update the field of old objects, not to create a new object. For example, don't create a new Font object in your own Paint () method. Instead, it should be declared as an example object, and then initialize once. After this, you can update it at any time in Paint (). See "Programming Pickup" edited by Bentley, P.81 [15]. ■ Abnormal: The abnormal processing module should only be discarded in an abnormal case. What is it called "abnormal"? This usually refers to a problem, which is generally unwilling to see, so performance is no longer a priority. When optimizing, the small "try-catch" block is combined together. Since these blocks split the code into small, their independent fragments, it hinders the compiler to optimize. On the other hand, if the excessive is keen to delete an abnormal processing module, it may also cause a decline in code. ■ Hash Treatment: First, the standard "Hashtable" class of Java 1.0 and 1.1 requires shape and synchronization processing of special consumption system resources (assignment time of 570 units). Second, early JDK libraries cannot automatically determine the best form size. Finally, the hash function should be designed for the characteristics of the actual use item (Key). Taking into account all these reasons, we can specifically design a hash class to cooperate with specific applications to improve the performance of conventional listings. Note that the HashMap of the Java 1.2 Collection Library has greater flexibility and will not automatically synchronize. ■ Method embedded: Only the Java compiler can embed this method in the case where the method belongs to Final (final), private (dedicated), or STATIC (static). Moreover, in some cases, it also requires it absolutely not a partial variable.

If the code spent a lot of time calls a method that does not contain any of the above properties, consider writing a "final" version. ■ I / O: The buffer should be used as much as possible. Otherwise, it will ultimately only input / output a byte of evil. Note that the I / O class of JDK 1.0 uses a large number of synchronization measures, so if you use a "large amount" call like ReadFully (), you can get better performance by explaining data yourself. It is also important to note that the "Reader" and "Writer" classes of Java 1.1 have been optimized for performance. ■ Modeling and instance: Modeling will consume 2 to 200 assignment time. The overhead is even asked to inherit (genetically) structure. Other high-cost operations will lose and restore the ability to restore lower-layer structures. ■ Graphics: Use shear technology to reduce the workload in repaint (); multiplier buffer, improve the receiving speed; simultaneously use graphic compression technology to shorten the download time. "Java Applets" from JavaWorld and "Performing Animation" from Sun are two good tutorials. Please remember the use of the most appropriate command. For example, to draw a polygon in accordance with a series of points, DrawPolygon () is much faster than DrawLine (). If you have to draw a single pixel thick line, Drawline (X, Y, X, Y) speed is faster than FillRect (X, Y, 1, 1). ■ Use the API class: Try to use classes from Java API because they have been optimized for the performance of the machine. This is hard to achieve with Java. For example, when copying any length of array, ArraryCopy () is much more than the speed of using the cycle. ■ Replace the API class: Sometimes, the API class provides more than what we want more, and the corresponding execution time will increase. Therefore, you can customize special versions, let it do less, but you can run faster. For example, assume that an application requires a container to save a large number of arrays. To speed up the execution speed, the original Vector (vector) can be replaced with a faster dynamic object array. 1. Other recommendations ■ Calculate the repeated constant to the critical cycle - such as buffer.length of the fixed length buffer. ■ static final (Static Final) constant helps the compiler optimization program. ■ Implement a fixed length cycle. ■ Use Javac optimization options: -o. It optimizes compiled code through embedded Static, Final, and Private methods. Attention The length of the class may increase (only the earlier version may not perform byte check) on the JDK 1.1 - earlier version). The new "Just-In-Time" compiler will dynamically accelerate the code. ■ minus the count to 0 ~ This uses a special JVM byte code.

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

New Post(0)