Skip to content

Instantly share code, notes, and snippets.

@konsolebox
Last active May 21, 2026 00:51
Show Gist options
  • Select an option

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

Select an option

Save konsolebox/1b157385392899c44f70fe0f2d780fd0 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Generated using Google AI
# Configuration
chunks=10000 # Number of iterations
chunk_size=15 # Average number of arguments per command
test_string="0123456789abcdef"
# Global tracking variables for results
baseline_time=0
total_arr=0
total_str1=0
total_str2=0
result_arr=""
result_str1=""
result_str2=""
# ========================================================
# TEST FUNCTIONS
# ========================================================
run_baseline() {
echo "Measuring combined data allocation baseline (Always First)..."
local start_baseline=$EPOCHREALTIME
local c i arr str1 str2
for ((c=0; c<chunks; c++)); do
arr=() str1="" str2=""
for ((i=0; i<chunk_size; i++)); do
arr+=("$test_string")
str1+=" $test_string"
str2+=" $test_string"
done
done
local end_baseline=$EPOCHREALTIME
baseline_time=$(bc <<< "$end_baseline - $start_baseline")
}
run_method1() {
echo "Running Method 1 (Array Append)..."
local start_time_arr=$EPOCHREALTIME
local c i arr
for ((c=0; c<chunks; c++)); do
arr=()
for ((i=0; i<chunk_size; i++)); do
arr+=("$test_string")
done
result_arr="${arr[*]}"
done
local end_time_arr=$EPOCHREALTIME
total_arr=$(bc <<< "$end_time_arr - $start_time_arr")
}
run_method2() {
echo "Running Method 2 (String Trim #)..."
local start_time_str1=$EPOCHREALTIME
local c i str
for ((c=0; c<chunks; c++)); do
str=""
for ((i=0; i<chunk_size; i++)); do
str+=" $test_string"
done
result_str1="${str# }"
done
local end_time_str1=$EPOCHREALTIME
total_str1=$(bc <<< "$end_time_str1 - $start_time_str1")
}
run_method3() {
echo "Running Method 3 (String Slice :1)..."
local start_time_str2=$EPOCHREALTIME
local c i str
for ((c=0; c<chunks; c++)); do
str=""
for ((i=0; i<chunk_size; i++)); do
str+=" $test_string"
done
result_str2="${str:1}"
done
local end_time_str2=$EPOCHREALTIME
total_str2=$(bc <<< "$end_time_str2 - $start_time_str2")
}
# ========================================================
# MAIN EXECUTION PLAN
# ========================================================
echo "Running $chunks rounds of $chunk_size arguments..."
echo "----------------------------------------"
# 1. Always execute the baseline first
run_baseline
# 2. Register competitive methods for shuffling
methods=(run_method1 run_method2 run_method3)
indices=("${!methods[@]}")
# Shuffle the competitive 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[@]} (Indices mapped to competitive methods)"
echo "----------------------------------------"
# 3. Execute the competitive methods in shuffled order
for idx in "${indices[@]}"; do
"${methods[idx]}"
done
# ========================================================
# RESULTS OUTPUT
# ========================================================
echo "----------------------------------------"
printf "Combined Data Baseline: %7.4fs\n" "$baseline_time"
echo "----------------------------------------"
printf "Method 1 (Array Append): %7.4fs\n" "$total_arr"
printf "Method 2 (String Trim #): %7.4fs\n" "$total_str1"
printf "Method 3 (String Slice :1): %7.4fs\n" "$total_str2"
echo "----------------------------------------"
# Integrity check
if [[ "$result_arr" == "$result_str1" && "$result_str1" == "$result_str2" ]]; then
echo "Success: All final outputs match perfectly."
else
echo "Error: Output mismatch discovered."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment