Skip to content

Instantly share code, notes, and snippets.

@konsolebox
Last active May 26, 2026 01:25
Show Gist options
  • Select an option

  • Save konsolebox/bf2a37c489292a092d878225e84b355b to your computer and use it in GitHub Desktop.

Select an option

Save konsolebox/bf2a37c489292a092d878225e84b355b to your computer and use it in GitHub Desktop.
#!/bin/bash
# Generated using Google AI
# Configuration
chunks=5000
test_args=(
# 1-5: Standard system/portage strings
"sys-apps/gentoo-functions"
"--verbose"
"USE=unicode"
"CHOST=x86_64-pc-linux-gnu"
"FEATURES=sandbox"
# 6-10: File paths and patterns
"/var/db/repos/gentoo"
"*.patch"
"./configure"
"MAKEOPTS=-j16"
"LDFLAGS=-Wl,-O1"
# 11-15: Escapes, spaces, quotes, and symbols
"escape_\E_sequence"
"There's more to life than this"
"space separated arg"
'nested"quotes"here'
"back\\slash\\test"
# 16-20: Dynamic expansion characters
'$variable_pattern'
"\${escaped_dollar}"
"simple"
"!!!"
"DONE=true"
)
# Global tracking variables
time_orig=0
time_prop=0
result_orig=""
result_prop=""
# ========================================================
# TARGET FUNCTIONS TO BENCHMARK
# ========================================================
_quote_args_bash_original() {
local IFS=" " args word
for word in "${@@Q}"; do
if [[ ${word} == \$* ]]; then
args+=("${word//\\E/\\e}")
else
args+=("${word}")
fi
done
printf %s\\n "${args[*]}"
}
_quote_args_bash_proposed() {
local str= qw qw2 w
for w; do
printf -v qw %q "$w" || return
if [[ ${qw} == "$w" ]]; then
str+=\ $w
elif [[ ${qw} == \$* ]]; then
str+=\ ${qw//\\E/\\e}
elif [[ $w == *\'* ]]; then
if printf -v qw2 %q $'\e'"$w" && [[ ${qw2} == \$\'\\[Ee]* ]]; then
str+=\ \$\'${qw2:4}
else
str+=\ ${qw}
fi
else
str+=" '$w'"
fi
done
printf %s\\n "${str# }"
}
# ========================================================
# BENCHMARK RUNNERS
# ========================================================
run_baseline() {
echo "Measuring loop overhead baseline (Always First)..."
local start_baseline=$EPOCHREALTIME
local c
for ((c=0; c<chunks; c++)); do
: $(_quote_args_bash_original "${test_args[@]}")
: $(_quote_args_bash_proposed "${test_args[@]}")
done
baseline_time=$(bc <<< "$EPOCHREALTIME - $start_baseline")
}
run_original() {
echo "Running Original Function (Array + \${@Q})..."
local start_time=$EPOCHREALTIME
local c
for ((c=0; c<chunks; c++)); do
result_orig=$(_quote_args_bash_original "${test_args[@]}")
done
time_orig=$(bc <<< "$EPOCHREALTIME - $start_time")
}
run_proposed() {
echo "Running Proposed Function (String Append + printf)..."
local start_time=$EPOCHREALTIME
local c
for ((c=0; c<chunks; c++)); do
result_prop=$(_quote_args_bash_proposed "${test_args[@]}")
done
time_prop=$(bc <<< "$EPOCHREALTIME - $start_time")
}
# ========================================================
# MAIN EXECUTION PLAN
# ========================================================
echo "Running $chunks rounds..."
echo "----------------------------------------"
# 1. Always execute the baseline first
run_baseline
# 2. Register and shuffle competitive methods
methods=(run_original run_proposed)
indices=("${!methods[@]}")
for ((i=${#indices[@]}-1; i>0; i--)); do
j=$(( RANDOM % (i + 1) ))
tmp=${indices[i]}
indices[i]=${indices[j]}
indices[j]=$tmp
done
echo "Test Execution Order: ${indices[@]}"
echo "----------------------------------------"
# 3. Execute methods
for idx in "${indices[@]}"; do
"${methods[idx]}"
done
# ========================================================
# RESULTS OUTPUT
# ========================================================
echo "----------------------------------------"
printf "Loop Overhead Baseline: %7.4fs\n" "$baseline_time"
echo "----------------------------------------"
printf "Original (Array Appends): %7.4fs\n" "$time_orig"
printf "Proposed (String Appends): %7.4fs\n" "$time_prop"
echo "----------------------------------------"
# Calculate Performance Penalty
# Formula: ((Proposed - Original) / Original) * 100
if (( $(bc <<< "$time_orig > 0") )); then
pct_slower=$(bc <<< "scale=2; (($time_prop - $time_orig) / $time_orig) * 100")
printf "Performance Result: Proposed is %s%% slower than Original.\n" "$pct_slower"
else
echo "Performance Result: Cannot calculate percentage (Original execution time too low)."
fi
echo "----------------------------------------"
# Visual Output Diff Verification
echo "Visualizing Output Differences:"
echo "----------------------------------------"
echo "Original Output Pattern:"
echo -n ">>> "
echo -n "$result_orig"
echo "----------------------------------------"
echo "Proposed Output Pattern:"
echo -n ">>> "
echo -n "$result_prop"
echo "----------------------------------------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment