Performance Boundaries
Loop Bound
Stop Work Early
Performance work starts by bounding how much work a loop can do. A clear limit keeps a script from processing more rows than the caller intended.
Program
Play the script to choose the loop limit and see how many items are processed.
loop_bound_plan.sh
#!/usr/bin/env bash
limit=
count=0
for i in 1 2 3 4 5; do
if [[ "$i" -gt "$limit" ]]; then
break
fi
count=$((count + 1))
done
echo "processed=$count limit=$limit"
#!/usr/bin/env bash
limit=
count=0
for i in 1 2 3 4 5; do
if [[ "$i" -gt "$limit" ]]; then
break
fi
count=$((count + 1))
done
echo "processed=$count limit=$limit"
loop bound
A loop bound limits how many iterations a script can perform.
early stop
`break` exits the loop once the boundary is reached.
work count
Counting processed items makes the boundary visible in output.