⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 optimizations.html

📁 Shall高级编程
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><HTML><HEAD><TITLE>Optimizations</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="Advanced Bash-Scripting Guide"HREF="index.html"><LINKREL="UP"TITLE="Miscellany"HREF="miscellany.html"><LINKREL="PREVIOUS"TITLE="Colorizing Scripts"HREF="colorizing.html"><LINKREL="NEXT"TITLE="Assorted Tips"HREF="assortedtips.html"><METAHTTP-EQUIV="Content-Style-Type"CONTENT="text/css"><LINKREL="stylesheet"HREF="common/kde-common.css"TYPE="text/css"><METAHTTP-EQUIV="Content-Type"CONTENT="text/html; charset=iso-8859-1"><METAHTTP-EQUIV="Content-Language"CONTENT="en"><LINKREL="stylesheet"HREF="common/kde-localised.css"TYPE="text/css"TITLE="KDE-English"><LINKREL="stylesheet"HREF="common/kde-default.css"TYPE="text/css"TITLE="KDE-Default"></HEAD><BODYCLASS="SECT1"BGCOLOR="#FFFFFF"TEXT="#000000"LINK="#AA0000"VLINK="#AA0055"ALINK="#AA0000"STYLE="font-family: sans-serif;"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="colorizing.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom">Chapter 33. Miscellany</TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="assortedtips.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><DIVCLASS="SECT1"><H1CLASS="SECT1"><ANAME="OPTIMIZATIONS"></A>33.7. Optimizations</H1><P>Most shell scripts are quick 'n dirty solutions to non-complex	  problems. As such, optimizing them for speed is not much of an	  issue.  Consider the case, though, where a script carries out	  an important task, does it well, but runs too slowly. Rewriting	  it in a compiled language may not be a palatable option. The	  simplest fix would be to rewrite the parts of the script	  that slow it down. Is it possible to apply principles of code	  optimization even to a lowly shell script?</P><P>Check the loops in the script. Time consumed by repetitive	  operations adds up quickly. If at all possible, remove	  time-consuming operations from within loops.</P><P>Use <AHREF="internal.html#BUILTINREF">builtin</A> commands in	  preference to system commands. Builtins execute faster and	  usually do not launch a subshell when invoked.</P><P><ANAME="CATABUSE"></A></P><P>Avoid unnecessary commands, particularly in a <AHREF="special-chars.html#PIPEREF">pipe</A>.	    <TABLEBORDER="0"BGCOLOR="#E0E0E0"WIDTH="100%"><TR><TD><PRECLASS="PROGRAMLISTING">   1&nbsp;cat "$file" | grep "$word"   2&nbsp;   3&nbsp;grep "$word" "$file"   4&nbsp;   5&nbsp;#  The above command lines have an identical effect,   6&nbsp;#+ but the second runs faster since it launches one fewer subprocess.</PRE></TD></TR></TABLE>	  The <AHREF="external.html#CATREF">cat</A> command seems especially	  prone to overuse in scripts.</P><P>Use the <AHREF="timedate.html#TIMREF">time</A> and <AHREF="internal.html#TIMESREF">times</A> tools to profile	  computation-intensive commands. Consider rewriting time-critical	  code sections in C, or even in assembler.</P><P>Try to minimize file I/O. Bash is not particularly	   efficient at handling files, so consider using	   more appropriate tools for this within the script,	   such as <AHREF="awk.html#AWKREF">awk</A> or <AHREF="wrapper.html#PERLREF">Perl</A>.</P><P>Write your scripts in a modular and coherent form,	   <ANAME="AEN18859"HREF="#FTN.AEN18859">[1]</A>	   so they can be reorganized and tightened up as necessary. Some	   of the optimization techniques applicable to high-level	   languages may work for scripts, but others, such as	   <ICLASS="FIRSTTERM">loop unrolling</I>, are mostly	   irrelevant. Above all, use common sense.</P><P>For an excellent demonstration of how optimization can	   dramatically reduce the  execution time of a script, see <AHREF="mathc.html#MONTHLYPMT">Example 15-45</A>.</P></DIV><H3CLASS="FOOTNOTES">Notes</H3><TABLEBORDER="0"CLASS="FOOTNOTES"WIDTH="100%"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="5%"><ANAME="FTN.AEN18859"HREF="optimizations.html#AEN18859">[1]</A></TD><TDALIGN="LEFT"VALIGN="TOP"WIDTH="95%"><P>This usually means liberal use of	   <AHREF="functions.html#FUNCTIONREF">functions</A>.</P></TD></TR></TABLE><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="colorizing.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="assortedtips.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><SPANCLASS="QUOTE">"Colorizing"</SPAN> Scripts</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="miscellany.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">Assorted Tips</TD></TR></TABLE></DIV></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -