tags:

views:

94

answers:

5

Hi, is there any nice GNU way how to measure average (worst case, best case) execution time of some command line program? I have image filter, unspecified amount of pictures, filtering them using for-loop in bash. So far I am using time, but I can't find a way how to get some statistics.

+2  A: 

You're on the right track with time. It's what I use to preform small code execution analyses.

I then use python to collect the statistics by reading the output of time. In order to increase accuracy, I typically do the trial 10 - 1000 times, depending on how long each process takes.

I'm not familiar with any pre-installed GNU application that does this sort of analysis.

Sean
If you're going to use Python, you might want to use [timeit](http://www.python.org/doc//current/library/timeit.html).
GreenMatt
Back in the old days, we ran the program 12 times using `time`, threw out the best and worst times, then averaged the remaining 10 values.
Loadmaster
I love timeit! I just recommend piping in the data from `time` because I don't want to have python controlling the execution of my commands and thereby potentially slowing the execution by capturing stdin and whatnot. It's kind of like, should I use C or should I use Python: That sort of feeling.
Sean
+4  A: 

You can send the output of time to some file, and then "work" that file

echo "some info" >> timefile.txt
time ( ./yourprog parm1 parm2 ) 2>> timefile.txt
pmg
You don't need the parentheses.
Dennis Williamson
On my computer (bash version 4.1.5(1)-release (x86_64-pc-linux-gnu)), without the parenthesis, the `2>>` applies to the inner program
pmg
+2  A: 
#!/bin/bash
for i in {1..100}
do
  env time --append -o time_output.txt   ./test_program --arguments-to-test-program
done
exit

If you find that the {1..100} syntax doesn't work for you then you should have a look at the seq command.

I used the env time to execute the time program rather than the shell's built in command, which does not take all of the arguments that the time program takes. The time program also takes other arguments to alter the format of it's output, which you will probably want to use to make the data easier to process by another program. The -p (--portability) argument makes it output in the POSIX format (like BASH's builtin time does), but using the -f option you can get more control. man 1 time for more info.

After you have gathered your data a simple perl or python script can easily parse and analyze your timing data.

nategoose
You don't have to use `seq`. You can do `for ((i=1; i<=100; i++))`.
Dennis Williamson
If possible, I want to avoid writing my own analyser if someone else did.
Dadam
+2  A: 

There's an interesting Perl program called dumbbench that's essentially a wrapper around the time command. It runs your program a number of times, throws away outliers, then calculates some statistics.

The author has a couple of articles (here and here) outlining a) why benchmarking sucks, and b) what kind of pretty graphs you can make to make your benchmarking numbers suck a little less.

CanSpice
I just hope I can pass different parameters for each run, I will test it tonight.
Dadam
A: 
Dennis Williamson