linuxlanguage-agnosticstatisticsprofilingbenchmarking

Calculate the average of several "time" commands in Linux


I'm profiling a program on Linux, using the "time" command. The problem is it's output is not very statistically relevant as it does only run the program once. Is there a tool or a way to get an average of several "time" runs? Possibly aswel together with statistical information such as deviation?


Solution

  • Here is a script I wrote to do something similar to what you are looking for. It runs the provided command 10 times, logging the real, user CPU and system CPU times to a file, and echoing tham after each command output. It then uses awk to provide averages of each of the 3 columns in the file, but does not (yet) include standard deviation.

    #!/bin/bash
    
    rm -f /tmp/mtime.$$
    
    for x in {1..10}
    do
      /usr/bin/time -f "real %e user %U sys %S" -a -o /tmp/mtime.$$ $@
      tail -1 /tmp/mtime.$$
    done
    
    awk '{ et += $2; ut += $4; st += $6; count++ } END {  printf "Average:\nreal %.3f user %.3f sys %.3f\n", et/count, ut/count, st/count }' /tmp/mtime.$$