Bash shell script for 100 iterations and log time taken

后端 未结 5 1301
长发绾君心
长发绾君心 2020-12-15 07:27

How do I run 100 iterations using a bash shell script? I want to know how long it will take to execute one command (start and end time). I want to keep track which iteratio

5条回答
  •  再見小時候
    2020-12-15 07:44

    This script is based on the answer from @marian0, but there's no limitation to the running time. Name it timeit.sh and then do ./timeit.sh 10 ./script to run ./script 10 times and print the average time. Additional arguments can be added as desired.

    #!/bin/bash
    
    for i in `seq 1 $1`; do
        time "${@:2}"
    done 2>&1 |\
        grep ^real |\
        sed -r -e "s/.*real\t([[:digit:]]+)m([[:digit:]]+\.[[:digit:]]+)s/\1 \2/" |\
        awk '{sum += $1 * 60 + $2} END {print sum / NR}'
    
    

提交回复
热议问题