1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # benchmarks/compute/control_flow.sh <function name>
|
5 |
|
6 | # Each of these 3 functions is a double loop that computes roughly n^2.
|
7 |
|
8 | do_continue() {
|
9 | local n=$1
|
10 | local i=0
|
11 | local sum=0
|
12 |
|
13 | while test $i -lt $n; do
|
14 | local j=0
|
15 |
|
16 | while test $j -lt $n; do
|
17 | j=$(( j + 1 ))
|
18 | sum=$((sum + 1))
|
19 |
|
20 | # This NO-OP continue penalizes OSH! It's almost as fast as bash without
|
21 | # it, but them becomes twice as slow.
|
22 |
|
23 | continue
|
24 | done
|
25 |
|
26 | i=$(( i + 1 ))
|
27 |
|
28 | done
|
29 |
|
30 | echo " sum=$sum"
|
31 | }
|
32 |
|
33 | do_break() {
|
34 | local n=$1
|
35 | local i=0
|
36 | local sum=0
|
37 |
|
38 | while test $i -lt $n; do
|
39 | local j=0
|
40 |
|
41 | while test $j -lt $n; do
|
42 | j=$(( j + 1 ))
|
43 | sum=$((sum + 1))
|
44 |
|
45 | # Extra break statement!
|
46 | if test $j -eq $i; then
|
47 | break;
|
48 | fi
|
49 | continue
|
50 | done
|
51 |
|
52 | i=$(( i + 1 ))
|
53 |
|
54 | done
|
55 |
|
56 | echo " sum=$sum"
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 | do_neither() {
|
62 | local n=$1
|
63 | local i=0
|
64 | local sum=0
|
65 |
|
66 | while test $i -lt $n; do
|
67 | local j=0
|
68 |
|
69 | while test $j -lt $n; do
|
70 | j=$(( j + 1 ))
|
71 | sum=$((sum + 1))
|
72 | done
|
73 |
|
74 | i=$(( i + 1 ))
|
75 |
|
76 | done
|
77 |
|
78 | echo " sum=$sum"
|
79 | }
|
80 |
|
81 | "$@"
|