appendixd.html
来自「java 是一个很好的网络开发环境。由于它是通过解释的方法」· HTML 代码 · 共 690 行 · 第 1/3 页
HTML
690 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<!--
This document was converted from RTF source:
By rtftohtml 4.19
See http://www.sunpack.com/RTF
Filename:Tjava14.rtf
Application Directory:c:\TOOLS\RTF2HTML\
Subject:
Author:Bruce Eckel
Operator:Bruce Eckel
Document Comments:
Version Comments:
Comments:
Keywords:
Translation Date:02/04/2000
Translation Time:23:26:30
Translation Platform:Win32
Number of Output files:27
This File:AppendixD.html
SplitDepth=1
SkipNavPanel=1
SkipLeadingToc=1
SkipTrailingToc=1
GenContents=1
GenFrames=1
GenIndex=1
-->
<HEAD lang="en"><META http-equiv="Content-Type" content="text/html">
<TITLE>D: Performance</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF"><DIV ALIGN="CENTER">
<a href="http://www.MindView.net">
<img src="mindview-head.gif" alt="MindView Inc." BORDER = "0"></a>
<CENTER>
<FONT FACE="Verdana" size = "-1">
[ <a href="README-HTML.txt">Viewing Hints</a> ]
[ <a href="http://www.mindview.net/TIJ2/index.html">2nd Edition</a> ]
[ <a href="http://www.mindview.net/MailingList.html">Free Newsletter</a> ] <br>
[ <a href="http://www.mindview.net/Training.html">Seminars</a> ]
[ <a href="http://www.mindview.net/javaCD2.html">Seminars on CD ROM</a> ]
[ <a href="http://www.mindview.net/CPPServices/#ConsultingServices">Consulting</a> ]
</FONT>
<H2><FONT FACE="Verdana">
Thinking in Java, 1st edition</FONT></H2>
<H3><FONT FACE="Verdana">©1998 by Bruce Eckel</FONT></H3>
<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>
</CENTER>
</P></DIV><A NAME="_Toc407441465"></A><A NAME="_Toc408018849"></A><A NAME="Heading633"></A><FONT FACE = "Verdana"><H1 ALIGN="LEFT">
D: Performance</H1></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Verdana" SIZE=4>This appendix was
contributed by and used with the permission of Joe Sharp, consultant
(SharpJoe@aol.com).</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><A NAME="Index3120"></A><FONT FACE="Georgia">The Java
language emphasizes accurate, reliable behavior at the expense of performance.
This is reflected in features such as automatic garbage collection, rigorous
runtime checking, complete byte code checking, and conservative runtime
synchronization. Availability on a wide choice of platforms leads, at present,
to an interpreted virtual machine that further handicaps performance.
</FONT>About performance, <FONT FACE="Georgia">Steve McConnell [16]
quoted: “Complete it first, and then perfect it. The part that needs to be
perfect is usually small.” This appendix will aid you in locating and
optimizing that “part that needs to be
perfect.”</FONT><A NAME="_Toc408018850"></A><BR></P></DIV>
<A NAME="Heading634"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Basic approach </H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You should address performance only
after you have a correct and fully tested program:</FONT><BR></P></DIV>
<OL>
<LI><FONT FACE="Georgia"> Measure the program’s performance under
realistic conditions. If it meets your requirements, you are finished. If not,
go to the next step.</FONT><LI><FONT FACE="Georgia"> Find the most
critical performance bottleneck. This might require considerable ingenuity, but
the effort will pay off. If you simply guess where the bottleneck is and try to
optimize there, you’ll waste your
time.</FONT><LI><FONT FACE="Georgia"> Apply the speed improvement
techniques discussed in this appendix, then return to Step
1.</FONT></OL><DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Finding the critical
bottleneck is the key to cost-effective effort – Donald Knuth [9] improved
a program where 50 percent of the time was spent in less than 4 percent of the
code. He changed a few lines in an hour of work and doubled the program speed.
Working on the rest of the program would have dissipated his valuable time and
effort. To quote Knuth, “Premature optimization is the root of all
evil.” It is wise to restrain your impulses to optimize early because you
may forgo many useful programming techniques, resulting in code that’s
harder to understand, riskier, and requires more effort to
maintain.</FONT><A NAME="_Toc408018851"></A><BR></P></DIV>
<A NAME="Heading635"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Locating the bottleneck</H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Three approaches to locating the
performance-critical part of a program
are:</FONT><A NAME="_Toc408018852"></A><BR></P></DIV>
<A NAME="Heading636"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
1. Install your own instrumentation</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">“Profile” code by
inserting explicit timing: </FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>long</font> start = System.currentTimeMillis();
<font color=#009900>// Operation to be timed goes here</font>
<font color=#0000ff>long</font> time = System.currentTimeMillis() - start;</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Have an infrequently-used method
print cumulative times out to the console window with
<B>System.out.println( ).</B> Since the compiler will ignore it when false,
a <B>static final boolean</B> switch can turn the timing on and off so the code
can efficiently be left in place in released code, ready for emergency use at
any time. Even when more sophisticated profiling is available, this is a
convenient way to time a specific task or operation.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>System.currentTimeMillis( )</B>
returns time in 1/1000ths of a second. However, some systems with time
resolution less than a millisecond (such as a Windows PC) need to repeat an
operation <B>n</B> times and divide the total time by <B>n</B> to get accurate
estimates. </FONT><A NAME="_Toc408018853"></A><BR></P></DIV>
<A NAME="Heading637"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
2. JDK profiling [2]<BR><A NAME="Index3121"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The JDK comes with a built-in
profiler that keeps track of the time spent in each routine and writes the
information to a file. Unfortunately, the JDK profilers have uneven performance.
JDK 1.1.1 works, but subsequent releases have had various
instabilities.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To run the profiler, use the
<B>-prof </B>option when invoking the unoptimized versions of the Java
interpreter, for example: </FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>java_g -prof myClass</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Or with an applet:
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>java_g -prof sun.applet.AppletViewer applet.html</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The profiler output is not
particularly easy to decipher. In fact, in JDK 1.0 it truncates the method names
to 30 characters, so it might not be possible to distinguish between some
methods. However, if your platform does support the <B>-prof</B> option, either
Vladimir Bulatov’s <I>HyperProf </I>[3] or Greg White’s
<I>ProfileViewer</I> [4] will help interpret the
results.</FONT><A NAME="_Toc408018854"></A><BR></P></DIV>
<A NAME="Heading638"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
3. Special tools</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The best way to keep up with the
exploding field of performance optimization tools is through a Web site such as
Jonathan Hardwick’s <I>Tools for Optimizing</I> <I>Java </I>[5] at
<I>http://www.cs.cmu.edu/~jch/java/tools.html</I>.</FONT><A NAME="_Toc408018855"></A><BR></P></DIV>
<A NAME="Heading639"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Tips for measuring performance</H3></FONT>
<UL>
<LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">Since profiling uses
clock time, make every effort to remove other processes during the
measurement.</FONT><LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">Always
time the code before and after making changes to verify that, at least on the
test platform, your changes improved the program. (Jon Bentley mentioned that
some of his most logical changes actually slowed the program
down.)</FONT><LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">Try to
make each timing test under identical conditions.
</FONT><LI><FONT FACE="Symbol"> </FONT><FONT FACE="Georgia">If possible,
contrive a test that doesn’t rely on any user input to avoid variations in
user response that can cause the results to fluctuate.
</FONT><A NAME="_Toc408018856"></A></UL><A NAME="Heading640"></A><FONT FACE = "Verdana"><H2 ALIGN="LEFT">
Speedup techniques </H2></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Now that the critical region has
been isolated, you can apply two types of
<A NAME="Index3122"></A>optimizations<B>: </B>generic techniques and those
specific to Java.</FONT><A NAME="_Toc408018857"></A><BR></P></DIV>
<A NAME="Heading641"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Generic approaches</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">An effective generic speedup is to
redefine the program in a more practical way. For example, in <I>Programming
Pearls</I> [14], Bentley describes Doug McIlroy’s representation of the
English language with a novel data depiction that enabled him to produce a
remarkably fast, compact spelling checker. In addition, choosing a better
algorithm will probably give a bigger performance gain than any other approach,
particularly as the size of the data set increases. For more of these generic
approaches, see the general book listings [12-19] at the end of this appendix.
</FONT><A NAME="_Toc408018858"></A><BR></P></DIV>
<A NAME="Heading642"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Language dependent approaches</H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">To put things in perspective,
it’s useful to look at the time it takes to perform various operations. So
that the results are relatively independent of the computer being used, they
have been normalized by dividing by the time it takes to make a local
assignment.</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><TABLE BORDER>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Operation</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Example</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Normalized time</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Local assignment</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>i = n;</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">1.0</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Instance
assignment</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>this.i = n;</B></FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">1.2</FONT><BR></P></DIV>
</TD>
</TR>
<TR VALIGN="TOP">
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>int</B>
increment</FONT><BR></P></DIV>
</TD>
<TD>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia"><B>i++;</B></FONT><BR></P></DIV>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?