PDA

View Full Version : Bash: timing a script



tg1w
November 30th, 2009, 08:28 PM
Hi

I need to benchmark a certain application and I want to use bash for this. I have already read that there is a /usr/bin/time program, apart from bash's own time command.

I have two questions:

/usr/bin/time outputs the "elapsed real time" and also the time spent by the program in user/kernel mode. Which of these is most relevant. If I am running other processes in the background, both elapsed time and user mode time are affected - there is no pure timing of a certain process
/usr/bin/time outputs to stderr. How may I collect the information in a variable (for example, with argument '/usr/bin/time -f "%e" some_program' I should obtain the execution time in seconds of "some_program". I cannot however say execution_time=/usr/bin/time -f "%e" some_program


Thank you all very much. PS: Still gaining experience in Ubuntu, so bare with me.

Martin Witte
November 30th, 2009, 09:27 PM
Maybe this link (http://tldp.org/HOWTO/KernelAnalysis-HOWTO-3.html) helps understanding te difference between kernel mode and user mode

tg1w
December 2nd, 2009, 10:35 AM
Maybe this link (http://tldp.org/HOWTO/KernelAnalysis-HOWTO-3.html) helps understanding te difference between kernel mode and user mode

Thank you very much. It makes now sense why an application run in user mode, will also involve kernel mode operations.

Would it sound reasonable then to time my simulation by summing the user mode and kernel mode times? Would it be more representative than just using the user mode?

Thank you all.

tg1w
December 2nd, 2009, 10:46 AM
/usr/bin/time outputs to stderr. How may I collect the information in a variable (for example, with argument '/usr/bin/time -f "%e" some_program' I should obtain the execution time in seconds of "some_program". I cannot however say execution_time=/usr/bin/time -f "%e" some_program

Arndt
December 2nd, 2009, 11:03 AM
/usr/bin/time outputs to stderr. How may I collect the information in a variable (for example, with argument '/usr/bin/time -f "%e" some_program' I should obtain the execution time in seconds of "some_program". I cannot however say execution_time=/usr/bin/time -f "%e" some_program

For example:


$ \time sleep 5 2>/tmp/timefile; execution_time=`cat /tmp/timefile`

There is also an --output option to 'time'. What do you want to happen if the program also outputs on stderr?

tg1w
December 2nd, 2009, 11:49 AM
For example:


$ \time sleep 5 2>/tmp/timefile; execution_time=`cat /tmp/timefile`

There is also an --output option to 'time'. What do you want to happen if the program also outputs on stderr?

Interesting solution. The above, I presume, uses bash's time function, rather than /usr/bin/time.

My main goal is to execute a number of simulations, time each of them individually and then display the total/average time.

I just read about times... maybe it would be better to use it, instead of doing arithmetical operations with time.

tg1w
December 2nd, 2009, 12:04 PM
Interesting solution. The above, I presume, uses bash's time function, rather than /usr/bin/time.


I wonder why using read doesn't solve the situation:



$ /usr/bin/time sleep 1 2>&1 | read -s sleep_time
$ echo $sleep_time

$

Arndt
December 2nd, 2009, 04:54 PM
Interesting solution. The above, I presume, uses bash's time function, rather than /usr/bin/time.




No, it does not. The backslash inhibits the builtin function. I just didn't want to spell out where the program is, even if it's unlikely to be somewhere else.

With bash's builtin time function, it's harder to redirect its output, maybe not possible.

tg1w
December 2nd, 2009, 11:16 PM
[QUOTE=Arndt;8425395]For example:


$ \time sleep 5 2>/tmp/timefile; execution_time=`cat /tmp/timefile`

Could you please help me out with the arithmetic operations too?

If I want to get only the user mode seconds, the results will be something like


$ \time -f "%U" sleep 1 2>/tmp/timefile; execution_time=`cat /tmp/timefile`
$ echo "$execution_time"
0.00
$


That x.xx is anything but something that I can add then to other simulation times. Bash also doesn't support float numbers.

Any help is welcomed. Thank you very much all.