tags:

views:

1253

answers:

4
+1  A: 

If you truly don't need stdout or stderr from the program being timed, this is a fine way to do this and should be as efficient as any other method.

Eddie
+4  A: 

the only change I would make is

var=$(time (mycommand &> /dev/null) 2>&1)

the $() command syntax if you shell supports it is superior for two reasons.

no need to escape backslashes and you can nest commands without escaping backticks.

see here for a description of the differences: Bash Command Substition

Jeremy Wall
+1 for preferring $(...) over `...`
Jonathan Leffler
+1  A: 

Only the mirror, because the primary seems to be down at the moment:

http://bash-hackers.org/wiki/doku.php/mirroring/bashfaq/032

From Greg's BashFAQ

TheBonsai
A: 

thanks! however, though it worked for me with linux,

   $ TIMEFORMAT="MYTIME %3R"
   $ time ./file_$ echo $TIME
   MYTIME 0.016
   $ TIME=`time ( ./file_tiny foo &> /dev/null ) 2>&1`
   $ echo $TIME
   MYTIME 0.016
   $

not so with cygwin:

   $ TIMEFORMAT="MYTIME %3R"
   $ time ./file_$ echo $TIME
   MYTIME 0.016
   $ TIME=`time ( ./file_tiny foo &> /dev/null ) 2>&1`
   $ echo $TIME
   $

thoughts? (besides "don't use cygwin" :-P it's too obvious.)