29

I am timeing some of my commands. Now to average the measures, I'd like to run and time my commands a specified number of times, and get the results with a calculated mean and standard deviation. The result would be like:

avgtime -n 100 my_command

real    0m5.388s stdev 0m0.068s
user    0m5.380s stdev 0m0.067s
sys     0m0.004s stdev 0m0.000s

Is there a UNIX tool for this? Does GNU/Linux have one?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Didier Trosset
  • 1,251
  • 1
  • 11
  • 13

6 Answers6

12

You can to try use the timeit module, available in any system with Python:

$ python -m timeit "__import__('os').system('my command here')"
10 loops, best of 3: 591 msec per loop
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
bhdnx
  • 251
  • 2
  • 6
7

Hyperfine is another option.

Sample usage:

hyperfine --warmup 3 'my_command'

See related question.

Luís Bianchin
  • 171
  • 1
  • 2
4

It's not exactly a UNIX or GNU/Linux tool, but you could quite comfortably use the R software environment for statistical computing for this. (I cannot find anything more specific for your task, though.)

Edit How could I doubt it, there of course is a benchmark package for R: rbenchmark. It apparently wraps system.time() which you could also just use directly. Or have a look at this, a simple stopwatch function pair. Also see "Executing a system command" @Rosetta Code (or don't, it's system("command").)

Edit2 I just saw this question, "Measuring time within a script" in the right "Related" column, this could be used, too, i.e. take time, do for-loop (N times), take time again, calculate timespan, divide by N. (Even easier, try time ( for-loop ), parse its output, divide by N).

sr_
  • 15,224
  • 49
  • 55
4

You can use R to quickly compute the mean, standard-deviation and other interesting values.

For example, you can use GNU time to write several runtime measurements into a CSV file:

function measure
{
  f=$1; shift
  n=$2; shift
  echo wall,user,sys,rss > "$f"
  for i in $(seq $n); do
    /usr/bin/time --output "$f" --append --format '%e,%U,%S,%M' "$@" >/dev/null
  done
}

Then you can generate the values with R like this:

Rscript --vanilla -e "b=read.csv(file='$f');summary(b);sapply(b, sd);"

I've created a small benchmark script that also does some pretty printing of the R output, e.g.:

$ benchmark.sh 100 ./some_prog arg1 arg2
n=100 | min    | Q1     | med    | mean   | Q3     | max    | std
wall  | 0.0400 | 0.0400 | 0.0500 | 0.0471 | 0.0500 | 0.0800 | 0.00624
user  | 0.0400 | 0.0400 | 0.0400 | 0.0426 | 0.0400 | 0.0700 | 0.00562
sys   | 0      | 0      | 0      | 0      | 0      | 0      | 0.00000
rss   | 2608   | 2657   | 2704   | 2728   | 2764   | 2920   | 95.06524
maxschlepzig
  • 56,316
  • 50
  • 205
  • 279
3

Two more options (both now available in debian et al.):

eMPee584
  • 327
  • 3
  • 7
1

Option 1 - sqlite:

create simple table with command and time columns, and view with proper aggregation calculations. After timing, add a row to the table.

Advantages: simpler to define a table compared to solution 2.

Disadvantages: you need (do you?) care about data retention.

Option 2 - rrdtool:

Define rrd data base file, data definition and aggregation functions. After timing, feed the database with rrdtool update ....

Advantages: you can easily generate graphs with rrdtool graph .... No data retention issue (round robin database).

Disadvantages: bit harder to define rrd database compared to simple SQL table/view

Michał Šrajer
  • 2,808
  • 17
  • 17