Debian Codegraph @

HTML/optimizations.html

raw symbols SCIP
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2 <HTML
3 ><HEAD
4 ><TITLE
5 >Optimizations</TITLE
6 ><META
7 NAME="GENERATOR"
8 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
9 "><LINK
10 REL="HOME"
11 TITLE="Advanced Bash-Scripting Guide"
12 HREF="index.html"><LINK
13 REL="UP"
14 TITLE="Miscellany"
15 HREF="miscellany.html"><LINK
16 REL="PREVIOUS"
17 TITLE="Colorizing Scripts"
18 HREF="colorizing.html"><LINK
19 REL="NEXT"
20 TITLE="Assorted Tips"
21 HREF="assortedtips.html"><META
22 HTTP-EQUIV="Content-Style-Type"
23 CONTENT="text/css"><LINK
24 REL="stylesheet"
25 HREF="common/kde-common.css"
26 TYPE="text/css"><META
27 HTTP-EQUIV="Content-Type"
28 CONTENT="text/html; charset=iso-8859-1"><META
29 HTTP-EQUIV="Content-Language"
30 CONTENT="en"><LINK
31 REL="stylesheet"
32 HREF="common/kde-localised.css"
33 TYPE="text/css"
34 TITLE="KDE-English"><LINK
35 REL="stylesheet"
36 HREF="common/kde-default.css"
37 TYPE="text/css"
38 TITLE="KDE-Default"></HEAD
39 ><BODY
40 CLASS="SECT1"
41 BGCOLOR="#FFFFFF"
42 TEXT="#000000"
43 LINK="#AA0000"
44 VLINK="#AA0055"
45 ALINK="#AA0000"
46 STYLE="font-family: sans-serif;"
47 ><DIV
48 CLASS="NAVHEADER"
49 ><TABLE
50 SUMMARY="Header navigation table"
51 WIDTH="100%"
52 BORDER="0"
53 CELLPADDING="0"
54 CELLSPACING="0"
55 ><TR
56 ><TH
57 COLSPAN="3"
58 ALIGN="center"
59 >Advanced Bash-Scripting Guide: An in-depth exploration of the art of shell scripting</TH
60 ></TR
61 ><TR
62 ><TD
63 WIDTH="10%"
64 ALIGN="left"
65 VALIGN="bottom"
66 ><A
67 HREF="colorizing.html"
68 ACCESSKEY="P"
69 >Prev</A
70 ></TD
71 ><TD
72 WIDTH="80%"
73 ALIGN="center"
74 VALIGN="bottom"
75 >Chapter 36. Miscellany</TD
76 ><TD
77 WIDTH="10%"
78 ALIGN="right"
79 VALIGN="bottom"
80 ><A
81 HREF="assortedtips.html"
82 ACCESSKEY="N"
83 >Next</A
84 ></TD
85 ></TR
86 ></TABLE
87 ><HR
88 ALIGN="LEFT"
89 WIDTH="100%"></DIV
90 ><DIV
91 CLASS="SECT1"
92 ><H1
93 CLASS="SECT1"
94 ><A
95 NAME="OPTIMIZATIONS"
96 ></A
97 >36.6. Optimizations</H1
98 ><P
99 >Most shell scripts are quick 'n dirty solutions to non-complex
100 problems. As such, optimizing them for speed is not much of an
101 issue. Consider the case, though, where a script carries out
102 an important task, does it well, but runs too slowly. Rewriting
103 it in a compiled language may not be a palatable option. The
104 simplest fix would be to rewrite the parts of the script
105 that slow it down. Is it possible to apply principles of code
106 optimization even to a lowly shell script?</P
107 ><P
108 >Check the loops in the script. Time consumed by repetitive
109 operations adds up quickly. If at all possible, remove
110 time-consuming operations from within loops.</P
111 ><P
112 >Use <A
113 HREF="internal.html#BUILTINREF"
114 >builtin</A
115 > commands in
116 preference to system commands. Builtins execute faster and
117 usually do not launch a subshell when invoked.</P
118 ><P
119 ><A
120 NAME="CATABUSE"
121 ></A
122 ></P
123 ><P
124 >Avoid unnecessary commands, particularly in a <A
125 HREF="special-chars.html#PIPEREF"
126 >pipe</A
127 >.
128 <TABLE
129 BORDER="0"
130 BGCOLOR="#E0E0E0"
131 WIDTH="100%"
132 ><TR
133 ><TD
134 ><PRE
135 CLASS="PROGRAMLISTING"
136 > 1&nbsp;cat "$file" | grep "$word"
137 2&nbsp;
138 3&nbsp;grep "$word" "$file"
139 4&nbsp;
140 5&nbsp;# The above command-lines have an identical effect,
141 6&nbsp;#+ but the second runs faster since it launches one fewer subprocess.</PRE
142 ></TD
143 ></TR
144 ></TABLE
145 >
146 The <A
147 HREF="external.html#CATREF"
148 >cat</A
149 > command seems especially
150 prone to overuse in scripts.</P
151 ><P
152 ><A
153 NAME="LCALL"
154 ></A
155 ></P
156 ><TABLE
157 CLASS="SIDEBAR"
158 BORDER="1"
159 CELLPADDING="5"
160 ><TR
161 ><TD
162 ><DIV
163 CLASS="SIDEBAR"
164 ><A
165 NAME="AEN20414"
166 ></A
167 ><P
168 >Disabling certain Bash options can speed up scripts.</P
169 ><P
170 >As Erik Brandsberg points out:</P
171 ><P
172 >If you don't need <A
173 HREF="bashver4.html#UNICODEREF"
174 >Unicode</A
175 > support, you can
176 get potentially a 2x or more improvement in speed by
177 simply setting the <TT
178 CLASS="USERINPUT"
179 ><B
180 >LC_ALL</B
181 ></TT
182 > variable.
183
184 <TABLE
185 BORDER="0"
186 BGCOLOR="#E0E0E0"
187 WIDTH="100%"
188 ><TR
189 ><TD
190 ><PRE
191 CLASS="PROGRAMLISTING"
192 > 1&nbsp; export LC_ALL=C
193 2&nbsp;
194 3&nbsp; [specifies the locale as ANSI C,
195 4&nbsp; thereby disabling Unicode support]
196 5&nbsp;
197 6&nbsp;[In an example script ...]
198 7&nbsp;
199 8&nbsp;Without [Unicode support]:
200 9&nbsp;erik@erik-desktop:~/capture$ time ./cap-ngrep.sh
201 10&nbsp;live2.pcap &#62; out.txt
202 11&nbsp;
203 12&nbsp; real 0m20.483s
204 13&nbsp; user 1m34.470s
205 14&nbsp; sys 0m12.869s
206 15&nbsp;
207 16&nbsp;With [Unicode support]:
208 17&nbsp;erik@erik-desktop:~/capture$ time ./cap-ngrep.sh
209 18&nbsp;live2.pcap &#62; out.txt
210 19&nbsp;
211 20&nbsp; real 0m50.232s
212 21&nbsp; user 3m51.118s
213 22&nbsp; sys 0m11.221s
214 23&nbsp;
215 24&nbsp;A large part of the overhead that is optimized is, I believe,
216 25&nbsp;regex match using [[ string =~ REGEX ]],
217 26&nbsp;but it may help with other portions of the code as well.
218 27&nbsp;I hadn't [seen it] mentioned that this optimization helped
219 28&nbsp;with Bash, but I had seen it helped with "grep,"
220 29&nbsp;so why not try?</PRE
221 ></TD
222 ></TR
223 ></TABLE
224 ></P
225 ></DIV
226 ></TD
227 ></TR
228 ></TABLE
229 ><P
230 ><A
231 NAME="OPTIMES"
232 ></A
233 ></P
234 ><DIV
235 CLASS="NOTE"
236 ><TABLE
237 CLASS="NOTE"
238 WIDTH="100%"
239 BORDER="0"
240 ><TR
241 ><TD
242 WIDTH="25"
243 ALIGN="CENTER"
244 VALIGN="TOP"
245 ><IMG
246 SRC="common/note.png"
247 HSPACE="5"
248 ALT="Note"></TD
249 ><TD
250 ALIGN="LEFT"
251 VALIGN="TOP"
252 ><P
253 >Certain operators, notably <A
254 HREF="moreadv.html#EXPRREF"
255 >expr</A
256 >, are very inefficient
257 and might be replaced by <A
258 HREF="dblparens.html"
259 >double
260 parentheses</A
261 > arithmetic expansion.
262 See <A
263 HREF="contributed-scripts.html#TESTEXECTIME"
264 >Example A-59</A
265 >.</P
266 ><P
267 ><TABLE
268 BORDER="0"
269 BGCOLOR="#E0E0E0"
270 WIDTH="100%"
271 ><TR
272 ><TD
273 ><PRE
274 CLASS="PROGRAMLISTING"
275 > 1&nbsp;Math tests
276 2&nbsp;
277 3&nbsp;math via $(( ))
278 4&nbsp;real 0m0.294s
279 5&nbsp;user 0m0.288s
280 6&nbsp;sys 0m0.008s
281 7&nbsp;
282 8&nbsp;math via expr:
283 9&nbsp;real 1m17.879s # Much slower!
284 10&nbsp;user 0m3.600s
285 11&nbsp;sys 0m8.765s
286 12&nbsp;
287 13&nbsp;math via let:
288 14&nbsp;real 0m0.364s
289 15&nbsp;user 0m0.372s
290 16&nbsp;sys 0m0.000s</PRE
291 ></TD
292 ></TR
293 ></TABLE
294 ></P
295 ><P
296 ><A
297 HREF="tests.html#IFTHEN"
298 >Condition testing</A
299 >
300 constructs in scripts deserve close scrutiny. Substitute
301 <A
302 HREF="testbranch.html#CASEESAC1"
303 >case</A
304 > for <A
305 HREF="tests.html#IFTHEN"
306 >if-then</A
307 > constructs and combine tests
308 when possible, to minimize script execution time. Again,
309 refer to <A
310 HREF="contributed-scripts.html#TESTEXECTIME"
311 >Example A-59</A
312 >.</P
313 ><P
314 ><TABLE
315 BORDER="0"
316 BGCOLOR="#E0E0E0"
317 WIDTH="100%"
318 ><TR
319 ><TD
320 ><PRE
321 CLASS="PROGRAMLISTING"
322 > 1&nbsp;Test using "case" construct:
323 2&nbsp;real 0m0.329s
324 3&nbsp;user 0m0.320s
325 4&nbsp;sys 0m0.000s
326 5&nbsp;
327 6&nbsp;
328 7&nbsp;Test with if [], no quotes:
329 8&nbsp;real 0m0.438s
330 9&nbsp;user 0m0.432s
331 10&nbsp;sys 0m0.008s
332 11&nbsp;
333 12&nbsp;
334 13&nbsp;Test with if [], quotes:
335 14&nbsp;real 0m0.476s
336 15&nbsp;user 0m0.452s
337 16&nbsp;sys 0m0.024s
338 17&nbsp;
339 18&nbsp;
340 19&nbsp;Test with if [], using -eq:
341 20&nbsp;real 0m0.457s
342 21&nbsp;user 0m0.456s
343 22&nbsp;sys 0m0.000s</PRE
344 ></TD
345 ></TR
346 ></TABLE
347 ></P
348 ></TD
349 ></TR
350 ></TABLE
351 ></DIV
352 ><P
353 ><A
354 NAME="ASSOCARRTST"
355 ></A
356 ></P
357 ><DIV
358 CLASS="NOTE"
359 ><TABLE
360 CLASS="NOTE"
361 WIDTH="100%"
362 BORDER="0"
363 ><TR
364 ><TD
365 WIDTH="25"
366 ALIGN="CENTER"
367 VALIGN="TOP"
368 ><IMG
369 SRC="common/note.png"
370 HSPACE="5"
371 ALT="Note"></TD
372 ><TD
373 ALIGN="LEFT"
374 VALIGN="TOP"
375 ><P
376 >Erik Brandsberg recommends using <A
377 HREF="bashver4.html#ASSOCARR"
378 >associative arrays</A
379 > in preference to
380 conventional numeric-indexed arrays in most cases. When
381 overwriting values in a numeric array, there is a significant
382 performance penalty vs. associative arrays. Running a test
383 script confirms this. See <A
384 HREF="contributed-scripts.html#ASSOCARRTEST"
385 >Example A-60</A
386 >.</P
387 ><P
388 ><TABLE
389 BORDER="0"
390 BGCOLOR="#E0E0E0"
391 WIDTH="100%"
392 ><TR
393 ><TD
394 ><PRE
395 CLASS="PROGRAMLISTING"
396 > 1&nbsp;Assignment tests
397 2&nbsp;
398 3&nbsp;Assigning a simple variable
399 4&nbsp;real 0m0.418s
400 5&nbsp;user 0m0.416s
401 6&nbsp;sys 0m0.004s
402 7&nbsp;
403 8&nbsp;Assigning a numeric index array entry
404 9&nbsp;real 0m0.582s
405 10&nbsp;user 0m0.564s
406 11&nbsp;sys 0m0.016s
407 12&nbsp;
408 13&nbsp;Overwriting a numeric index array entry
409 14&nbsp;real 0m21.931s
410 15&nbsp;user 0m21.913s
411 16&nbsp;sys 0m0.016s
412 17&nbsp;
413 18&nbsp;Linear reading of numeric index array
414 19&nbsp;real 0m0.422s
415 20&nbsp;user 0m0.416s
416 21&nbsp;sys 0m0.004s
417 22&nbsp;
418 23&nbsp;Assigning an associative array entry
419 24&nbsp;real 0m1.800s
420 25&nbsp;user 0m1.796s
421 26&nbsp;sys 0m0.004s
422 27&nbsp;
423 28&nbsp;Overwriting an associative array entry
424 29&nbsp;real 0m1.798s
425 30&nbsp;user 0m1.784s
426 31&nbsp;sys 0m0.012s
427 32&nbsp;
428 33&nbsp;Linear reading an associative array entry
429 34&nbsp;real 0m0.420s
430 35&nbsp;user 0m0.420s
431 36&nbsp;sys 0m0.000s
432 37&nbsp;
433 38&nbsp;Assigning a random number to a simple variable
434 39&nbsp;real 0m0.402s
435 40&nbsp;user 0m0.388s
436 41&nbsp;sys 0m0.016s
437 42&nbsp;
438 43&nbsp;Assigning a sparse numeric index array entry randomly into 64k cells
439 44&nbsp;real 0m12.678s
440 45&nbsp;user 0m12.649s
441 46&nbsp;sys 0m0.028s
442 47&nbsp;
443 48&nbsp;Reading sparse numeric index array entry
444 49&nbsp;real 0m0.087s
445 50&nbsp;user 0m0.084s
446 51&nbsp;sys 0m0.000s
447 52&nbsp;
448 53&nbsp;Assigning a sparse associative array entry randomly into 64k cells
449 54&nbsp;real 0m0.698s
450 55&nbsp;user 0m0.696s
451 56&nbsp;sys 0m0.004s
452 57&nbsp;
453 58&nbsp;Reading sparse associative index array entry
454 59&nbsp;real 0m0.083s
455 60&nbsp;user 0m0.084s
456 61&nbsp;sys 0m0.000s</PRE
457 ></TD
458 ></TR
459 ></TABLE
460 ></P
461 ></TD
462 ></TR
463 ></TABLE
464 ></DIV
465 ><P
466 >Use the <A
467 HREF="timedate.html#TIMREF"
468 >time</A
469 > and <A
470 HREF="internal.html#TIMESREF"
471 >times</A
472 > tools to profile
473 computation-intensive commands. Consider rewriting time-critical
474 code sections in C, or even in assembler.</P
475 ><P
476 >Try to minimize file I/O. Bash is not particularly
477 efficient at handling files, so consider using
478 more appropriate tools for this within the script,
479 such as <A
480 HREF="awk.html#AWKREF"
481 >awk</A
482 > or <A
483 HREF="wrapper.html#PERLREF"
484 >Perl</A
485 >.</P
486 ><P
487 >Write your scripts in a modular and coherent form,
488 <A
489 NAME="AEN20452"
490 HREF="#FTN.AEN20452"
491 >[1]</A
492 >
493 so they can be reorganized and tightened up as necessary. Some
494 of the optimization techniques applicable to high-level
495 languages may work for scripts, but others, such as
496 <I
497 CLASS="FIRSTTERM"
498 >loop unrolling</I
499 >, are mostly
500 irrelevant. Above all, use common sense.</P
501 ><P
502 >For an excellent demonstration of how optimization can
503 dramatically reduce the execution time of a script, see <A
504 HREF="mathc.html#MONTHLYPMT"
505 >Example 16-47</A
506 >.</P
507 ></DIV
508 ><H3
509 CLASS="FOOTNOTES"
510 >Notes</H3
511 ><TABLE
512 BORDER="0"
513 CLASS="FOOTNOTES"
514 WIDTH="100%"
515 ><TR
516 ><TD
517 ALIGN="LEFT"
518 VALIGN="TOP"
519 WIDTH="5%"
520 ><A
521 NAME="FTN.AEN20452"
522 HREF="optimizations.html#AEN20452"
523 >[1]</A
524 ></TD
525 ><TD
526 ALIGN="LEFT"
527 VALIGN="TOP"
528 WIDTH="95%"
529 ><P
530 >This usually means liberal use of
531 <A
532 HREF="functions.html#FUNCTIONREF"
533 >functions</A
534 >.</P
535 ></TD
536 ></TR
537 ></TABLE
538 ><DIV
539 CLASS="NAVFOOTER"
540 ><HR
541 ALIGN="LEFT"
542 WIDTH="100%"><TABLE
543 SUMMARY="Footer navigation table"
544 WIDTH="100%"
545 BORDER="0"
546 CELLPADDING="0"
547 CELLSPACING="0"
548 ><TR
549 ><TD
550 WIDTH="33%"
551 ALIGN="left"
552 VALIGN="top"
553 ><A
554 HREF="colorizing.html"
555 ACCESSKEY="P"
556 >Prev</A
557 ></TD
558 ><TD
559 WIDTH="34%"
560 ALIGN="center"
561 VALIGN="top"
562 ><A
563 HREF="index.html"
564 ACCESSKEY="H"
565 >Home</A
566 ></TD
567 ><TD
568 WIDTH="33%"
569 ALIGN="right"
570 VALIGN="top"
571 ><A
572 HREF="assortedtips.html"
573 ACCESSKEY="N"
574 >Next</A
575 ></TD
576 ></TR
577 ><TR
578 ><TD
579 WIDTH="33%"
580 ALIGN="left"
581 VALIGN="top"
582 ><SPAN
583 CLASS="QUOTE"
584 >"Colorizing"</SPAN
585 > Scripts</TD
586 ><TD
587 WIDTH="34%"
588 ALIGN="center"
589 VALIGN="top"
590 ><A
591 HREF="miscellany.html"
592 ACCESSKEY="U"
593 >Up</A
594 ></TD
595 ><TD
596 WIDTH="33%"
597 ALIGN="right"
598 VALIGN="top"
599 >Assorted Tips</TD
600 ></TR
601 ></TABLE
602 ></DIV
603 ></BODY
604 ></HTML
605 >