#!/bin/bash benchmark() { total_real_time=0 total_user_time=0 total_system_time=0 for ((i=0; i<$1; i++)); do printf "\r%0${#1}d/%0${#1}d %s" $((i + 1)) $1 "$2" >&2 time_slept=($( (TIMEFORMAT="%R %U %S"; time $2 1>/dev/null) |& tr -d . )) ((total_real_time+=$(printf '%d' $(echo ${time_slept[0]} | sed 's/^0*//')))) ((total_user_time+=$(printf '%d' $(echo ${time_slept[1]} | sed 's/^0*//')))) ((total_system_time+=$(printf '%d' $(echo ${time_slept[2]} | sed 's/^0*//')))) done printf "\n" >&2 echo "Total real time: ${total_real_time}ms; Average real time: $(bc <<< "scale=3; $total_real_time / $1")ms" echo "Total user time: ${total_user_time}ms; Average user time: $(bc <<< "scale=3; $total_user_time / $1")ms" echo "Total system time: ${total_system_time}ms; Average system time: $(bc <<< "scale=3; $total_system_time / $1")ms" } "$@" # benchmark 3 'sleep 1s' # benchmark 3 'python3 ../../Python/code_cycle.py' # benchmark 10 '../../Rust/code_cycle/target/release/code_cycle'