Java performance

xiaoxiao2021-04-07  340

"This appendix is ​​submitted by Joe Sharp and has been reproduced here. Please contact Sharpjoe@aol.com"

The Java language emphasizes accuracy, but reliable behavior is based on performance. This feature is reflected in the automatic collection of garbage, strict period test, complete bytecode check, and conservative period synchronization. For an interpreted virtual machine, since there are currently a large number of platforms available, it is further hindered by performance.

"Let it finish it, then improve. Fortunately, it is usually not too much place to improve." (Steve McConnell "About Performance" [16])

The purpose of this appendix is ​​to guide everyone to find and optimize the "part of the need to be perfect".

1 basic method

Only after the programs are detected correctly and completely, they can solve performance problems:

(1) Detect 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.

2 look for bottlenecks

In order to find bottlenecks that most influencing program performance, several methods can be taken:

1) Invert your test code

Insert the "explicit" timing code to evaluate the program:

Long Start = system.currenttimemillis ();

// The calculation code to be timed here is here.

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.

2) JDK performance evaluation [2]

The JDK supporting provides a built-in evaluation program that tracks the time spent on each routine and writes the evaluation results 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. E.g:

Java_g -prof myclass

Or plus a program (Applet):

Java_g -prof sun.applet.appletViewer applet.html

It is not easy to understand the output information of the evaluation process. 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.

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) for Jonathan Hardwick:

Http://www.cs.cmu.edu/~jch/java/tools.html

4) Skills for performance evaluation

■ Since the system clock is used during evaluation, do not run any other processes or applications at that time, so as not to affect 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.

3 speed test

Now, the key performance bottleneck should be isolated. Next, two types of optimizations can be applied: routine means and dependent on Java languages.

1) Regular means

Typically, an effective speed 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.

2) Dependency

For objective analysis, it is best to clearly grasp 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

Example assignment this.i = n; 1.2

INT value-added i ; 1.5

BYTE value-added B ; 2.0

SHORT value-added S ; 2.0

Float value value F ; 2.0

Double value D ; 2.0

Empty Circulation While (True) N ; 2.0

Trimed expression (x <0)? -X: x 2.2

Math.abs (x); 2.5

Array assignment a [0] = N; 2.7

LONG value-added L ; 3.5 method call funct (); 5.9

THROW or CATCH exception Try {throw e;} or catch (e) {} 320

Synchronous method calls synchmehod (); 570

New object new Object (); 980

New Array New Int INT [10]; 3100

Through your own system (such as my Pentium 200 Pro, Netscape 3 and JDK 1.1.5), these relative time to everyone: New objects and arrays will cause the most heavy overhead, synchronization will cause more heavy overhead, and once Uncapacity method calls can cause moderate overhead. Reference Resources [5] and [6] summarizes the web address of the measuring program, you can run them on your own machine.

Conventional modification

Below is some normal operation recommendations that speed up the speed of the Java program (note the test results before and after the modification).

Will modify ...

Interface abstract classes (only one parent) interfaces of multiple inherits hinder performance optimization

Non-local or array cyclic variables local cycle variables according to the time consumption of the previous table, the time of the integer assignment is 1.2 times the local integer assignment time, but the time the array assignment is 2.7 times the value of the local integer.

Link list (fixed size) Save the discarded link item, or replace the list to a loop array (generally known size) Each new object is applied to the local assignment of 980 times. Refer to "Reuse Use Object" (next section), Van Wyk [12] P.87 and Bentley [15] P.81

X / 2 (or 2 of any power) X >> 2 (or 2 of any power) use faster hardware instructions

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, adds an argument, and then converts the result back to a string with toString (). 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 special consumption system resource synchronization (570 unit assignment time). 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.

The method is embedded: only the Java compiler can embed this method in the case where the method belongs to 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: The model consumes 2 to 200 units of 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 workload in repaint (); multiplier buffer, improve reception speed; simultaneously utilize graphical compression technology, 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, and the corresponding execution time will also 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.

Other suggestions

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.

Realize the cycle of fixed length.

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.

Reduce the count to 0 ~ This uses a special JVM byte code.

4 reference resources

1) Performance Tools

[1] MicroBenchmark running in Pentium Pro 200, Netscape 3.0, JDK 1.1.4 (see the reference resource below [5])

[2] Sun's Java documentation page - JDK Java Interpreter Topic:

http://java.sun.com/products/jdk/tools/win32/java.html

[3] VLADIMIR BULATOV HYPERPROF

http://www.physics.orst.edu/~bulatov/hyperprof

[4] Greg White's ProfileViewer

http://www.inetmi.com/~gwhi/profileviewer/profileviewer.html

2) Web site

[5] For the optimal theme of Java code, the best online reference resources are Jonathan Hardwick's "Java Optimization" website:

Http://www.cs.cmu.edu/~jch/java/optimization.html

Java Optimization Tools Homepage:

Http://www.cs.cmu.edu/~jch/java/tools.html

And "Java Microbenchmarks" (with a 45 second evaluation process):

Http://www.cs.cmu.edu/~jch/java/benchmarks.html

3) Article

[6] "Make Java Fast: Optimize! How to get the greatest Performanceout of Your Code Through low-level Optimizations in java" (let Java faster: optimize! How to optimize the code in Java, so that the code exhibits the best performance). Author: Doug Bell. Website:

http://www.javaworld.com/javaworld/jw-04-1997/jw-04-Optimize.html

(Including a comprehensive performance evaluation program, detailed note) [7] "Java Optimization Resources" (Java optimized resources)

Http://www.cs.cmu.edu/~jch/java/resources.html

[8] "Optimizing Java for Speed" (optimized Java, increase speed):

http://www.cs.cmu.edu/~jch/java/speed.html

[9] "An Empirical Study of Fortran Program" (Fortran program actual combat parsing). Author: Donald Knuth. Published in 1971. Volume 1, P.105-33, "Software - Practice and Exercise".

[10] "Building High-Performance Applications and Servers in Java: An Experiential Study". Author: Jimmy Nguyen, Michael Fraenkel, RichardRedpath, Binh Q. Nguyen and Sandeep K. Singhal. IBM T.J. Watson ResearchCenter, IBM Software Solutions.

http://www.ibm.com/java/education/javahipr.html

4) Java professional books

[11] "Advanced Java, Idioms, Pitfalls, Styles, and Programming Tips". Author: Chris Laffra. Prentice Hall published in 1997 (Java 1.0). Chapter 11, No. 20.

5) General books

[12] "Data Structures and C Programs" (data structure and C procedures). Author: J.Van Wyk. Addison-Wesly published in 1998.

[13] "Writing Efficient Programs" (written a valid program). Author: Jon Bentley. Prentice Hall published in 1982. Special references to P.110 and P.145-151.

[14] "More Programming Pearls" (Programming Pickup Second Edition). Author: JonBentley. "Association for Computing Machinery", February 1998.

[15] "Programming Pearls". Author: Jone Bentley. Addison-Wesley published in 1989. Part 2 emphasizes the problem of conventional performance improvement. [16] "Code Complete: a Practical Handbook of Software Construction" (Full Code Index: Practical Software Development Manual). Author: Steve McConnell. Microsoft Press published in 1993, Chapter 9.

[17] "Object-Oriented System Development" (Development of Object System). Author: Champeaux, Lea and Faure. Chapter 25.

[18] "THE ART OF Program". Author: Donald Knuth. Volume 1 "The 3rd Edition of the Basic Algorithm"; Volume 3 "Sort and Search 2nd Edition". Addison-Wesley Published. This is an encyclopedia of the program algorithm. [19] "Algorithms In C: Fundammentals, Data Structures, Sorting, Searching" (C Algorithm: Foundation, Data Structure, Sort, Search). Author: RobertSedgewick. Addison-Wesley published in 1997. The author is a student of Knuth. This is one of the seven versions of several languages. The algorithm has been discharged in depth.

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

New Post(0)