From 45d6c999cc5852cd59f23f250fcf3672232038a3 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Sat, 12 Oct 2024 14:58:06 -0300 Subject: bin/muffle: Speed up First, use sort -m, as both inputs are already sorted. This both changes from it from O(n*log(n)) to O(n), and allows sort(1) to produce streaming output, instead of sponging all the input, and potentially spilling to disk during the process. Second, use awk(1) when looping. Not only awk(1) is made for this type of job, but also we only call it once and let it do the loop, over looping manually and calling '['(1), printf() and cut(1) on each iteration. Now we call cut(1) only once. It went from being annoying enough to make me pause considering using to being unnoticeable. --- bin/muffle | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) (limited to 'bin') diff --git a/bin/muffle b/bin/muffle index 0feed18..5cf0bdd 100755 --- a/bin/muffle +++ b/bin/muffle @@ -81,18 +81,13 @@ timed() { echo 0 > "$STATUS_F" { - { "$@" || echo $? > "$STATUS_F"; } | timed | pre out > "$OUT" -} 2>&1 | timed | pre err > "$ERR" + { "$@" || echo $? > "$STATUS_F"; } | pre -n out | timed > "$OUT" +} 2>&1 | pre -n err | timed > "$ERR" STATUS="$(cat "$STATUS_F")" if [ "$INVERT" = false -a "$STATUS" != 0 ] || [ "$INVERT" = true -a "$STATUS" = 0 ]; then - cat "$OUT" "$ERR" | - sort -k2 | while read -r line; do - if [ "$(printf '%s\n' "$line" | cut -d' ' -f1)" = 'out:' ]; then - printf '%s\n' "$line" | cut -d' ' -f3- >&1 - else - printf '%s\n' "$line" | cut -d' ' -f3- >&2 - fi - done + sort -m "$OUT" "$ERR" | cut -d' ' -f2- | awk '{ + print substr($0, length($1) + 2) > "/dev/std" $1 + }' exit "$STATUS" fi -- cgit v1.2.3