How to measure average execution time of a script?

You can run iterations of the program in a loop; and divide the total time by the number of iterations:

time for i in {1..10}; do sleep 1; done
real    0m10.052s
user    0m0.005s
sys 0m0.018s

there is a tool called multitime that does exactly this: running a command several times, measuring how long it takes (real/user/system with mean, min/max and median time automatically computed)

For instance, for measuring a similar script 100 times:

multitime -q -n 100 "fact1.sh"
===> multitime results
1: -q fact1.sh
            Mean        Std.Dev.    Min         Median      Max
real        0.122       0.032       0.086       0.116       0.171       
user        0.148       0.044       0.096       0.137       0.223       
sys         0.023       0.019       0.000       0.014       0.061 

This is old but it came up so high on google when I was looking for a command I used previously but couldn't find. Anyways, my preferred way of doing this is:

perf stat -r 10 -B sleep 1

This gives quite a bit of details including average execution time right at the end:

1.002248382 seconds time elapsed                   ( +-  0.01% )

Tags:

Linux