Try this, it runs a simple command with arguments and puts the times
$real $user $sys and preserves the exit code.
It also does not fork subshells or trample on any variables except real
user sys, and does not otherwise interfere with the running of the
script
e.g.
note: it only times a simple command, not any part of a pipeline (all parts of which are run in a sub-shell).
This version allows you to specify as $1 $2 $3 the name of the variables that should receive the 3 times:
e.g.
and may be useful if it ends up being called recursively, to avoid trampling on times; but then r u s etc should be declared local in their use.
Note:
Shared here: http://unix.stackexchange.com/a/257964/139357
timer () {
{ time { "$@" ; } 2>${_} {_}>&- ; } {_}>&2 2>"/tmp/$$.$BASHPID.${#FUNCNAME[@]}"
set -- $?
read -d "" _ real _ user _ sys _ < "/tmp/$$.$BASHPID.${#FUNCNAME[@]}"
rm -f "/tmp/$$.$BASHPID.${#FUNCNAME[@]}"
return $1
}
e.g.
timer find /bin /sbin /usr rm /tmp/
echo $real $user $sys
note: it only times a simple command, not any part of a pipeline (all parts of which are run in a sub-shell).
This version allows you to specify as $1 $2 $3 the name of the variables that should receive the 3 times:
timer () {
{ time { "${@:4}" ; } 2>${_} {_}>&- ; } {_}>&2 2>"/tmp/$$.$BASHPID.${#FUNCNAME[@]}"
set -- $? "$@"
read -d "" _ "$2" _ "$3" _ "$4" _ < "/tmp/$$.$BASHPID.${#FUNCNAME[@]}"
rm -f "/tmp/$$.$BASHPID.${#FUNCNAME[@]}"
return $1
}
e.g.
timer r u s find /bin /sbin /usr rm /tmp/
echo $r $u $s
and may be useful if it ends up being called recursively, to avoid trampling on times; but then r u s etc should be declared local in their use.
Note:
"/tmp/$$.$BASHPID.${#FUNCNAME[@]}"
is a way of specifying a temporary file name that will not be trampled on until after this function exits.Shared here: http://unix.stackexchange.com/a/257964/139357
No comments:
Post a Comment