Measure shell performance
Performance measurement compares implementations that meet the same contract. Wall-clock time includes waiting and scheduling; user CPU time measures computation in user space and system CPU time measures kernel work. Bash time measures a command or pipeline, including builtins and functions, and TIMEFORMAT configures its report. An external executable normally needs process startup and program loading, while a builtin can run in the current shell. This difference is often important in loops but does not mean every builtin-based algorithm is faster.
A reproducible experiment
Use the same inputs, output bytes, interpreter and environment. Verify output before timing. Warm both implementations, alternate the order across repeated trials and report all observations plus a median, not a single lucky run. The median is the middle value after sorting (or the mean of the middle two for an even count). Record workload size, machine, locale, versions and measurement method. Compare a loop using builtin printf against one using /usr/bin/printf, then against one batched builtin call. A measured improvement only applies to this workload. Real scripts may be dominated by I/O, network latency or different computation. Do not infer memory use from elapsed time. The accompanying benchmark runner records real timings separately from deterministic lesson outputs. The timing example validates the report shape, not a fixed duration. The timing drill checks the timed command and the report read before reporting success. The public benchmark is available at lab-fixtures/v28/benchmark.py; see lab-fixtures/v59/README.en-fa.md. Its timings include shell startup and output capture.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. jq, GNU coreutils, GNU tar and flock are external dependencies for selected examples. Use only synthetic files in a private directory.
Compare equivalent implementations
for i in 1 2 3; do builtin printf '%s\n' "$i" || exit 1; done > builtin.txt || exit 1for i in 1 2 3; do /usr/bin/printf '%s\n' "$i" || exit 1; done > external.txt || exit 1cmp -s builtin.txt external.txt || exit 1printf 'same bytes\n' Expected stdout:
same bytesFirst establish semantic equivalence, then measure. The external path is specific to this lab.
Validate a timing report
TIMEFORMAT='elapsed=%3R'{ time printf 'ok\n' > output.txt; } 2> timing.txt || exit 1IFS= read -r measurement < timing.txt || exit 1[[ $measurement =~ ^elapsed=[0-9]+\.[0-9]{3}$ ]] || exit 1cat output.txt || exit 1printf 'timing recorded\n' Expected stdout:
oktiming recordedThe timing report goes to stderr, captured separately. Its actual value depends on the run.
Reduce repeated calls
values=(alpha 'daily report' omega)for v in "${values[@]}"; do printf '%s\n' "$v" || exit 1; done > loop.txt || exit 1printf '%s\n' "${values[@]}" > batch.txt || exit 1cmp -s loop.txt batch.txt || exit 1cat batch.txtExpected stdout:
alphadaily reportomegaThe format is reused for each argument. This array is nonempty; an empty array requires an explicit guard if zero records are expected.
Companion lab: lab-fixtures/v28/benchmark.py; measured results: source-evidence/v28/benchmark-results.json.
Common mistakes
Do not compare different outputs or claim a universal speed ratio from one machine. Avoid replacing a clear command with a fragile optimization before measuring the actual bottleneck.
Practice
Run the companion benchmark with 100 and 500 records. Record medians and ranges; check identical bytes and explain startup cost versus batching. Do not assert a fixed speedup or memory reduction.
Next: Streaming, batching and memory