Skip to content

Instantly share code, notes, and snippets.

@jason810496
Created February 15, 2025 10:54
Show Gist options
  • Select an option

  • Save jason810496/11b208fb964e0c490a6cd82f7d1882f7 to your computer and use it in GitHub Desktop.

Select an option

Save jason810496/11b208fb964e0c490a6cd82f7d1882f7 to your computer and use it in GitHub Desktop.
Simple script for benchmarking.
#!/bin/bash
if [ "$#" -eq 0 ]; then
echo "Usage: $0 <command>"
exit 1
fi
TOTAL_TIME=0
NUM_RUNS=5
echo -e "Running: $@ ($NUM_RUNS times)\n"
OUTPUT=""
for i in $(seq 1 $NUM_RUNS); do
START_TIME=$(date +%s.%N)
"$@"
END_TIME=$(date +%s.%N)
ELAPSED_TIME=$(awk "BEGIN {print $END_TIME - $START_TIME}")
TOTAL_TIME=$(awk "BEGIN {print $TOTAL_TIME + $ELAPSED_TIME}")
OUTPUT+="Run $i: ${ELAPSED_TIME} seconds\n"
done
AVERAGE_TIME=$(awk "BEGIN {print $TOTAL_TIME / $NUM_RUNS}")
OUTPUT+="Average time: ${AVERAGE_TIME} seconds"
echo -e "$OUTPUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment