-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaster_sh.txt
652 lines (434 loc) · 9.23 KB
/
faster_sh.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#http://www.los-gatos.ca.us/davidbu/faster_sh.html
David Butcher: Speeding Up Your UNIX Shell Scripts
David Butcher: Speeding Up Your UNIX Shell Scripts
Who would not want to speed up their shell scripts? Here are some simple
tips to make your shell scripts run faster.
Disclaimer: These techniques have made my UNIX shell
scripts faster, on my hardware and OS. They may not work for you. Program
at your own risk. YMMV (Your mileage may vary) Speedup factors are
approximate. Bourne Shell only. Changing your code to conform to these
examples may have side effects (particularly when variables are set in
subshells by one code path and not by the other). Implementation details
are left to the reader. Some speedups are based in part on using memory-
mapped files (ramdisks). Sometimes these scripts can dramatically affect
the performance of the test system while they are running, in a negative
way. You have been warned.
Issue: Reading successive lines from a file using a "while" loop.
Code:
:
cd /tmp
exec 3<&0
A1(){
while read A
do
:
done < /tmp/somefile
}
A2(){
exec 0< /tmp/somefile
while read A
do
:
done
exec 0<&3
}
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for k in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A1
done
done
done
# end of script
Results in seconds:
A1
real 37.1
user 4.9
sys 30.6
A2
real 8.5
user 3.2
sys 5.3
Conclusion: 5X speedup
Use file descriptor manipulation instead of input
redirection when using a loop to read from a file.
Issue: Reading successive lines from the output of a command
using a "while" loop.
:
cd /tmp
exec 3<&0
A1(){
cal | while read LINE
do
:
done
}
A2(){
cal > /tmp/fast$$
exec 0< /tmp/fast$$
while read LINE
do
:
done
exec 0<&3
}
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A1
done
done
# end of script
# Results in seconds:
A1
real 19.3
user 1.5
sys 12.2
A2
real 6.6
user 1.2
sys 4.3
Conclusion: 3X speedup
Use file descriptor manipulation and a temporary
file to hold results of the command output
instead of pipes when using a loop to read output from
a command.
Issue: Appending output to a file from within a loop.
:
cd /tmp
A1(){
echo "\c" >> /tmp/tt$$
}
A2(){
echo "\c"
}
exec 3<&1
exec 1>>/tmp/tt$$
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for k in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for l in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A2
done
done
done
done
exec 1<&3
# end of script
rm /tmp/tt$$
# Results in seconds:
# A1
# real 1:03.7
# user 26.7
# sys 36.9
# A2
# real 10.7
# user 10.6
# sys 0.0
Conclusion: 6X speedup
Always perform file output around the outside of the loop, instead
of opening and closing the file multiple times within the loop. Use
file descriptor manipulation to avoid running the loop in a subshell.
NOTE: in A1 above, the file descriptor manipulation is not used. Test
times were generated for A1 without the exec's.
Issue: Testing for a particular integer value.
:
cd /tmp
A=1
A1(){
[ "$A" = 1 ]
}
A2(){
[ "$A" -eq 1 ]
}
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for k in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for l in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A2
done
done
done
done
# end of script
# Results in seconds:
# A1
# real 14.9
# user 14.9
# sys 0.0
# A2
# real 17.8
# user 17.6
# sys 0.0
Conclusion: 15% speedup
When testing integer equality, the string operator "=" is slightly faster
than the arithmetic operator "-eq". Be careful, though, because "=" will
deny that "1" is equal to "01", and "-eq" will get it right.
Issue: Testing for a particular integer value.
:
cd /tmp
A=1
A1(){
if [ "$A" = 1 ]
then
B="$A"
fi
}
A2(){
case "$A" in
1)B="$A";;
esac
}
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for k in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for l in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A1
done
done
done
done
# end of script
# Results in seconds:
# A1
# real 19.6
# user 19.6
# sys 0.0
# A2
# real 12.3
# user 12.3
# sys 0.0
Conclusion: 35% speedup
When testing integer equality, "case" is quite a bit faster than "test."
Be careful, though, because "case: will deny that "1" is equal to "01",
and "test" using the arithmetic operator "-eq" will get it right.
Issue: Testing multiple equality conditions, string or integer.
:
cd /tmp
A=1
B=1
A1(){
if [ "$A" = 1 -a "$B" = 1 ]
then
C="$A"
fi
}
A2(){
case "$A$B" in
11)C="$A";;
esac
}
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for k in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for l in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A2
done
done
done
done
# end of script
# Results in seconds:
# A1
# real 26.6
# user 26.4
# sys 0.0
# A2
# real 13.5
# user 13.5
# sys 0.0
Conclusion: 2X speedup (or more with more conditions)
When testing for multiple conditions, "case" is much faster
than "test." The more conditions to be simultaneously compared, the
bigger the speedup. Case statements make excellent replacements for
"if then" statements which must test multiple conditions simultaneously.
Issue: Placing the names in the current directory in a variable.
:
A1(){
set -- *
FILES="$*"
}
A2(){
FILES=`echo *`
}
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for k in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A2
done
done
done
# end of script
exit
# Results in seconds:
# A1
real 9.5
user 4.8
sys 4.6
# A2
real 52.9
user 8.9
sys 41.9
Conclusion: 5X speedup
Use 'set -- [wildcards]' to make current directory filenames available
for variable assignment through $*.
Issue: Setting and reading a "lock file."
:
A1(){
while [ -s lock_file ]
do
# should be sleep here in "real" program
# with a timeout if necessary to prevent sleeping "forever"
:
done
# acquire the lock
echo "$$" > lock_file
# verify that we got it, someone else could have just tried to
# acquire it as well
read MY_PID < lock_file
case "$MY_PID" in
$$)
# we have the lock, execute the program
:
# after program is complete, clear the lock
> lock_file
;;
esac
}
A2(){
while [ -s lock_file ]
do
# should be sleep here in "real" program
# with a timeout if necessary to prevent sleeping "forever"
:
done
# acquire the lock
echo "$$" > lock_file
# verify that we got it, someone else could have just tried to
# acquire it as well
read MY_PID < lock_file
case "$MY_PID" in
$$)
# we have the lock, execute the program
:
# after program is complete, remove the lock
rm lock_file
;;
esac
}
for i in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for j in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
for k in 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 0
do
A2
done
done
done
# end of script
exit
# Results in seconds:
# A1
real 4.5
user 3.9
sys 0.3
# A2
real 43.1
user 22.2
sys 15.8
Conclusion: 10X speedup
If possible, leave lock files in place, and check to see if they have
contents to begin the process of setting the lock. Do not erase the lock
file between program runs, as the 'rm' command is expensive, and checking
for non-existent files exercises more of the file system code than checking
for existing files, especially if the existing file is in the filesystem
cache.
Note: Any scripts presented below this line were tested on Linux using
GNU bash, version 2.05.0(1)-release (i386-suse-linux)
Copyright 2000 Free Software Foundation, Inc.
Issue: Performing work inline versus calling a function.
# program with inline variable set
:
for h in 1 2 3 4 5 6 7 8 9 10
do
for i in 1 2 3 4 5 6 7 8 9 10
do
for j in 1 2 3 4 5 6 7 8 9 10
do
for k in 1 2 3 4 5 6 7 8 9 10
do
a=1
done
done
done
done
# end of script
exit
# program with function
:
A(){
a=1
}
for h in 1 2 3 4 5 6 7 8 9 10
do
for i in 1 2 3 4 5 6 7 8 9 10
do
for j in 1 2 3 4 5 6 7 8 9 10
do
for k in 1 2 3 4 5 6 7 8 9 10
do
A
done
done
done
done
# end of script
exit
# Results in seconds:
# Variable Set inline
real 0m0.379s
user 0m0.310s
sys 0m0.010s
# Variable set in a function
real 0m0.921s
user 0m0.790s
sys 0m0.000s
Conclusion: 3X speedup
Unless there is a compelling reason to call a function, and
there typically ARE many compelling reasons to place code
in functions, you will see significantly faster execution if
the code is simply typed inline in your program. Of course,
this will make the most difference in overall execution time
if the code is called repeatedly, as it is in the example above.
Coincidentally, that is one of the reasons code is placed in
functions: so it can be called repeatedly WITHOUT copying it
inline everywhere. Exercise common sense on this speedup.
Back To:
[ David Butcher's Personal Page ]
This page is hosted by
The WEBworks
* Copyright 1998, All Rights Reserved