appendixd.html
来自「java 是一个很好的网络开发环境。由于它是通过解释的方法」· HTML 代码 · 共 690 行 · 第 1/3 页
HTML
690 行
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Saving discarded link items or
replacing the list with a circular array (in which approximate size is
known)</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each new object takes 980 local
assignments. See Reusing Objects (below), Van Wyk [12] p. 87 and Bentley[15] p.
81</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">x/2 (or any power of
2)</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">X >> 2
</FONT><BR><FONT FACE="Georgia">(or any power of 2)</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Uses faster hardware
instructions</FONT><BR></P></DIV>
</TD>
</TR>
<A NAME="_Toc408018859"></A></TABLE></P></DIV>
<A NAME="Heading644"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Specific situations</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>The cost of Strings</B>: The
<B>String</B> concatenation operator + looks innocent but involves a lot of
work. The compiler can efficiently concatenate constant strings, but a variable
string requires considerable processing. For example, if <B>s</B> and <B>t</B>
are <B>String</B> variables:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>System.out.println(<font color=#004488>"heading"</font> + s + <font color=#004488>"trailer"</font> + t);</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">this requires a new
<B>StringBuffer</B>, appending arguments, and converting the result back to a
<B>String </B>with <B>toString( )</B>. This costs both space and time. If
you’re appending more than one <B>String</B>, consider using a
<B>StringBuffer</B> directly, especially if you can repeatedly reuse it in a
loop. Preventing the creation of a new <B>StringBuffer</B> on each iteration
saves the object creation time of 980 seen earlier. Using
<B>substring( )</B> and the other <B>String</B> methods is usually an
improvement. When feasible, character arrays are even faster. Also notice that
<B>StringTokenizer</B> is costly because of synchronization.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Synchronization</B>: In the JDK
interpreter, calling a <B>synchronized</B> method is typically 10 times slower
than calling an unsynchronized method. With JIT compilers, this performance gap
has increased to 50 to 100 times (notice the timing above shows it to be 97
times slower). Avoid <B>synchronized</B> methods if you can – if you
can’t, synchronizing on methods rather than on code blocks is slightly
faster.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Reusing objects</B>: It takes a
long time to create an object (the timing above shows 980 assignment times for a
new <B>Object</B>, and 3100 assignment times for a small new array), so
it’s often worth saving and updating the fields of an old object instead
of creating a new object. For example, rather than creating a new <B>Font</B>
object in your <B>paint( )</B> method, you can declare it an instance
object, initialize it once, and then just update it when necessary in
<B>paint( )</B>. See also Bentley, <I>Programming Pearls</I> p. 81
[15].</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Exceptions</B>: You should only
throw exceptions in abnormal situations, which are usually cases in which
performance is not an issue since the program has run into a problem that it
doesn’t normally expect. When optimizing, combine small
<B>try</B>-<B>catch</B> blocks, which thwart compiler optimization by breaking
the code into small independent sections. On the other hand, be careful of
sacrificing the robustness of your code by over-zealous removal of exception
handling.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Hashing: </B>The standard
<B>Hashtable</B> class in Java 1.0 and 1.1 requires casting and costly
synchronization (570 assignment times). Furthermore, the early JDK library
doesn’t deliberately choose prime number table sizes. Finally, a hashing
function should be designed for the particular characteristics of the keys
actually used. For all these reasons, the generic <B>Hashtable</B> can be
improved by designing a hash class that fits a particular application<B>.</B>
Note that the <B>HashMap</B> in the Java 1.2 collections library has much
greater flexibility and isn’t automatically
<B>synchronized</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Method inlining:</B> Java
compilers can inline a method only if it is <B>final</B>, <B>private</B>, or
<B>static</B>, and in some cases it must have no local variables. If your code
spends a lot of time calling a method that has none of these modifiers, consider
writing a version that is <B>final</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>I/O</B>: Use buffers wherever
possible, otherwise you can end up doing I/O a single byte at a time. Note that
the JDK 1.0 I/O classes use a lot of synchronization, so you might get better
performance by using a single “bulk” call such as
<B>readFully( )</B> and then interpreting the data yourself. Also notice
that the Java 1.1 “reader” and “writer” classes were
designed for improved performance.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Casts and instanceof</B>:<B>
</B>Casts take from 2 to 200 assignment times. The more costly ones require
travel up the inheritance hierarchy. Other costly operations lose and restore
capabilities of the lower level constructs.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Graphics:</B> Use clipping to
reduce the amount of work done in <B>repaint( )</B>, double buffering to
improve perceived speed, and image strips or compression to speed downloading
times. <I>Animation in Java Applets</I> from JavaWorld and <I>Performing
Animation</I> from Sun are two good tutorials. Remember to use high-level
primitives; it’s much faster to call <B>drawPolygon( )</B> on a bunch
of points than looping with <B>drawLine( )</B>. If you must draw a
one-pixel-wide line, <B>drawLine(x,y,x,y)</B> is faster than
<B>fillRect(x,y,1,1)</B>.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Using API classes</B>: Use
classes from the Java API when they offer native machine performance that you
can’t match using Java. For example, <B>arrayCopy( )</B> is much
faster than using a loop to copy an array of any significant size.
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>Replacing API classes</B>:
Sometimes API classes do more than you need, with a corresponding increase in
execution time. For these you can write specialized versions that do less but
run faster. For example, one application that needed a container to store many
arrays was speeded by replacing the original <B>Vector</B> with a faster dynamic
array of objects.</FONT><BR></P></DIV>
<A NAME="Heading645"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Other suggestions</H4></FONT>
<UL>
<LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">Move repeated
constant calculations out of a critical loop, for example, computing
<B>buffer.length</B> for a constant-size
<B>buffer</B>.</FONT><LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia"><B>static
final </B>constants can help the compiler optimize the program.
</FONT><LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">Unroll fixed
length
loops.</FONT><LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">Use
javac’s optimization option, <B>-O</B>, which optimizes compiled code by
inlining <B>static</B>, <B>final</B>, and <B>private</B> methods. Note that your
classes may get larger in size (JDK 1.1 or later only – earlier versions
might not perform byte verification). Newer just-in-time (JIT) compilers will
dramatically speed the
code.</FONT><LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">Count
down to zero whenever possible – this uses a special JVM byte
code.</FONT><A NAME="_Toc408018860"></A></UL><A NAME="Heading646"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
References<A NAME="_Toc408018861"></A></H2></FONT>
<A NAME="Heading647"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Performance tools</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[1] MicroBenchmark running on
Pentium Pro (200Mh), Netscape 3.0, JDK 1.1.4 (see reference [5]
below).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[2] Sun’s Java document page
on the JDK Java interpreter
<I>http://java.sun.com/products/JDK/tools/win32/java.html</I></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[3] Vladimir Bulatov’s
<I>HyperProf</I>
<I>http://www.physics.orst.edu/~bulatov/HyperProf</I></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[4] Greg White’s
<I>ProfileViewer</I>
<I>http://www.inetmi.com/~gwhi/ProfileViewer/ProfileViewer.html</I></FONT><A NAME="_Toc408018862"></A><BR></P></DIV>
<A NAME="Heading648"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Web sites</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[5] The premiere online references
for optimizing Java code are Jonathan Hardwick’s Java Optimization<I>
</I>site at <I>http://www.cs.cmu.edu/~jch/java/optimization.html</I>,
“Tools for Optimizing Java” at
<I>http://www.cs.cmu.edu/~jch/java/tools.html</I>, and “Java
Microbenchmarks” (with a quick 45 second measurement benchmark) at
<I>http://www.cs.cmu.edu/~jch/java/benchmarks.html.</I>
</FONT><A NAME="_Toc408018863"></A><BR></P></DIV>
<A NAME="Heading649"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Articles</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[6] <I>Make Java fast: Optimize!
How to get the greatest performance out of your code through low-level
optimizations in Java </I>by<I> </I>Doug Bell
<I>http://www.javaworld.com/javaworld/jw-04-1997/jw-04-optimize.html</I>,
complete with an extensive annotated measurement Benchmark
applet.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[7] <I>Java Optimization
Resources</I>
<I>http://www.cs.cmu.edu/~jch/java/resources.html</I></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[8] <I>Optimizing Java for
Speed</I> <I>http://www.cs.cmu.edu/~jch/java/speed.html</I></FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[9] <I>An Empirical Study of
FORTRAN Programs</I> by Donald Knuth, 1971, Software – Practice and
Experience, Volume 1 p. 105-33.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[10] <I>Building High-Performance
Applications and Servers in Java: An Experiential Study</I>, by Jimmy Nguyen,
Michael Fraenkel, Richard Redpath, Binh Q. Nguyen, and Sandeep K. Singhal; IBM
Software Solutions, IBM T.J. Watson Research Center.
<I>http://www.ibm.com/java/education/javahipr.html.</I></FONT><A NAME="_Toc408018864"></A><BR></P></DIV>
<A NAME="Heading650"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Java specific books</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[11] <I>Advanced Java, Idioms,
Pitfalls, Styles, and Programming Tips</I>, by Chris<I> </I>Laffra, Prentice
Hall, 1997. (Java 1.0) Chapter Sections
11-20.</FONT><A NAME="_Toc408018865"></A><BR></P></DIV>
<A NAME="Heading651"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
General books</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[12] <I>Data Structures and C
Programs</I> by Christopher J. Van Wyk, Addison-Wesley, 1988.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[13] <I>Writing Efficient Programs
</I>by Jon Bentley, Prentice Hall, 1982, especially p. 110 and p.
145-151.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[14] <I>More Programming Pearls</I>
by Jon Bentley. Association for Computing Machinery, February
1988.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[15] <I>Programming Pearls</I> by
Jon Bentley, Addison-Wesley 1989. Part II addresses generic performance
enhancements.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[16] <I>Code Complete: A Practical
Handbook of Software Construction </I>by Steve McConnell, Microsoft Press 1993,
Chapter 9.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[17] <I>Object-Oriented System
Development</I> by Champeaux, Lea, and Faure, Chapter 25.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[18] <I>The Art of Programming</I>
by Donald Knuth, Volume 1 <I>Fundamental Algorithms </I>3<SUP>rd</SUP> Edition,
1997; Volume 2, <I>Seminumerical Algorithms</I> 3<SUP>rd</SUP> Edition; Volume 3
<I>Sorting and Searching</I> 2<SUP>nd</SUP> Edition, Addison-Wesley. The
definitive encyclopedia of algorithms.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">[19] <I>Algorithms in C:
Fundamentals, Data Structures, Sorting, Searching</I> by Robert Sedgewick,
3<SUP>rd</SUP> Edition, Addison-Wesley 1997. The author is an apprentice of
Knuth’s. This is one of seven editions devoted to several languages and
contains timely, somewhat simpler treatments of
algorithms.</FONT><A NAME="Appendix_D"></A><A NAME="_Toc375545510"></A><BR></P></DIV>
<DIV ALIGN="CENTER">
<FONT FACE="Verdana" size = "-1">
[ <a href="AppendixC.html">Previous Chapter</a> ]
[ <a href="SimpleContents.html">Short TOC</a> ]
[ <a href="Contents.html">Table of Contents</a> ]
[ <a href="DocIndex.html">Index</a> ]
[ <a href="AppendixE.html">Next Chapter</a> ]
</FONT>
<BR>
Last Update:02/04/2000</P></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?