$ time if [[ $dir2 -ot $dir1 ]] ; then open $dir2 ; else open $dir1 ; fi
real 0m0.055s
user 0m0.023s
sys 0m0.018s
$ time [[ $dir2 -ot $dir1 ]] && open $dir2 || open $dir1
real 0m0.000s
user 0m0.000s
sys 0m0.000s
At above, I compared two methods for testing which directory is older and then opening the older directory. I guess I'm just confused why the &&
and ||
operators right after a test has the same functionality of an if
/then
test, and why it's faster. Are there advantages/disadvantages to each?
Best Answer
time
always times the directly following pipeline.if ... fi
is a single compound command, and[[ ... ]]
is one command. The part after&&
is not measured bytime
becausefoo && bar
is a list of commands.Compare:
To measure time from all of the second commands, you can group them by putting it in (curly) brackets: