summaryrefslogtreecommitdiffstats
path: root/builtin_bench.sh
blob: 8fb604a6a4761699c231fc1129a6f7467eed0ee6 (plain)
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
#!/usr/bin/env bash
#
# @author David Palma <david@davidpalma.me>
# Benchmark Bash builtins against external programs

set -o nounset
DEBUG=${DEBUG:-0}

# Print real time in seconds
TIMEFORMAT='%R'

bash_version_check() {
    local running=${BASH_VERSION:0:1}
    local required=4
    if [[ $running -lt $required ]]; then
        echo 'WARN: This benchmarks relies on features in Bash 4.0+.' >&2
        echo "Please install a newer version than ${running}." >&2
        return 1
    fi
    return 0
}

benchmark() {
    local count="$1"
    local cmd="$2"
    if [[ $DEBUG -eq 1 ]]; then
        # Test output is correct
        echo "DEBUG: ${cmd}" >&2
        eval $cmd >&2
    fi
    time for i in $(seq ${count}); do
        eval $cmd >/dev/null
    done
    return 0
}

print_benchmark() {
    local iterations="$1"

    printf 'builtin echo: '
    benchmark "${iterations}" 'builtin echo'

    printf '/bin/echo: '
    benchmark "${iterations}" '/bin/echo'

    printf 'builtin printf: '
    benchmark "${iterations}" 'builtin printf "\n"'

    printf '/usr/bin/printf: '
    benchmark "${iterations}" '/usr/bin/printf "\n"'
    return 0
}

math_benchmark() {
    local iterations="$1"
    readonly integer=987

    printf 'builtin shell math: '
    benchmark "${iterations}" 'echo $(( ${integer} + 1 ))'

    printf '/bin/expr: '
    benchmark "${iterations}" '/bin/expr ${integer} + 1'

    printf '/usr/bin/bc: '
    benchmark "${iterations}" 'echo "${integer} + 1" | bc'
    return 0
}

read_benchmark() {
    local iterations="$1"

    printf 'builtin redirection: '
    benchmark "${iterations}" '< /etc/services'

    printf '/usr/bin/cat: '
    benchmark "${iterations}" 'cat /etc/services'
    return 0
}

date_benchmark() {
    local iterations="$1"

    printf 'builtin printf: '
    benchmark "${iterations}" 'printf "%(%Y-%m-%d)T\n"'

    printf '/usr/bin/date: '
    benchmark "${iterations}" 'date +%F'
    return 0
}

command_benchmark() {
    local iterations="$1"

    printf 'builtin hash: '
    benchmark "${iterations}" 'hash ls'

    printf '/usr/bin/which: '
    benchmark "${iterations}" '/usr/bin/which ls'
    return 0
}

seq_expansion_benchmark() {
    local iterations="$1"
    readonly limit=1000

    printf 'builtin sequence expansion: '
    benchmark "${iterations}" 'eval echo {1..${limit}}'

    printf 'seq: '
    benchmark "${iterations}" 'seq ${limit}'
    return 0
}

replacement_benchmark() {
    local iterations="$1"
    readonly REPLACE='string_replacement'

    printf 'builtin variable replacement: '
    benchmark "${iterations}" 'echo ${REPLACE/placement/transform}'

    printf '/usr/bin/sed: '
    benchmark "${iterations}" 'echo ${REPLACE} | sed -e "s/placement/transform/"'
    return 0
}

downcase_benchmark() {
    local iterations="$1"
    readonly UPPER='CRUISECONTROL'

    printf 'builtin variable manipulation: '
    benchmark "${iterations}" 'echo ${UPPER,,}'

    printf '/usr/bin/tr: '
    benchmark "${iterations}" 'echo ${UPPER} | tr [A-Z] [a-z]'

    printf '/usr/bin/awk: '
    benchmark "${iterations}" 'echo ${UPPER} | awk "{print tolower(\$0)}"'
    return 0
}

charlength_benchmark() {
    local iterations="$1"
    readonly CONTENT='Remember kids, use your builtins!'

    printf 'builtin variable length: '
    benchmark "${iterations}" 'echo "${#CONTENT}"'

    printf '/usr/bin/wc -c: '
    benchmark "${iterations}" 'printf "${CONTENT}" | wc -c'
    return 0
}

linelength_benchmark() {
    local iterations="$1"
    readonly MULTILINE=$(printf 'Remember kids,\nUse your builtins!\nCheers,\n@mistofvongola\n')

    # Note that we convert the variable to an array first
    printf 'builtin mapfile and array length: '
    benchmark "${iterations}" 'mapfile -t MLARR <<< "${MULTILINE}" && echo "${#MLARR[@]}"'

    printf '/usr/bin/wc -l: '
    benchmark "${iterations}" 'echo "${MULTILINE}" | wc -l'
    return 0
}

basename_benchmark() {
    local iterations="$1"
    readonly basefile='/etc/services'

    printf 'builtin variable manipulation: '
    benchmark "${iterations}" 'echo ${basefile##*/}'

    printf '/usr/bin/basename: '
    benchmark "${iterations}" '/usr/bin/basename ${basefile}'
    return 0
}

dirname_benchmark() {
    local iterations="$1"
    readonly dirfile='/etc/services'

    printf 'builtin variable manipulation: '
    benchmark "${iterations}" 'echo ${dirfile%/*}'

    printf '/usr/bin/dirname: '
    benchmark "${iterations}" '/usr/bin/dirname ${dirfile}'
    return 0
}

main() {
    # check args
    if [ $# -ne 1 ]; then
        echo "usage: $0 <number>"
        return 1
    elif [[ $1 =~ ^[0-9]+$ ]]; then
        local count="$1"
    else
        echo "usage: $0 <number>"
        return 1
    fi

    [[ $DEBUG -eq 1 ]] && echo "DEBUG mode enabled" >&2
    bash_version_check || return 2

    echo '>>> print_benchmark'
    print_benchmark "${count}"
    echo

    echo '>>> math_benchmark'
    math_benchmark "${count}"
    echo

    echo '>>> read_benchmark'
    read_benchmark "${count}"
    echo

    echo '>>> date_benchmark'
    date_benchmark "${count}"
    echo

    echo '>>> command_benchmark'
    command_benchmark "${count}"
    echo

    echo '>>> seq_expansion_benchmark'
    seq_expansion_benchmark "${count}"
    echo

    echo '>>> replacement_benchmark'
    replacement_benchmark "${count}"
    echo

    echo '>>> downcase_benchmark'
    downcase_benchmark "${count}"
    echo

    echo '>>> charlength_benchmark'
    charlength_benchmark "${count}"
    echo

    echo '>>> linelength_benchmark'
    linelength_benchmark "${count}"
    echo

    echo '>>> basename_benchmark'
    basename_benchmark "${count}"
    echo

    echo '>>> dirname_benchmark'
    dirname_benchmark "${count}"
    echo
    return 0
}

main "$@"
exit $?