Well technically I don't *have* to accept the performance penalty.
As I can just use the posix comform syntax, which is quicker.
And just becasue I was curious as to how much quicker it is. Now
admittedly it's not the biggest difference in perf, but I don't see any
real point in using a slower one.
test_file_1(){
test -f "${1:?Missing Test File}" -a -x "${1}"
}
test_file_2(){
test -f "${1:?Missing Test File}" && test -x "${1}"
}
test_case(){
local cnt=${1:?Missing repeat count}
local test_func=${2:?Missing Test Function}
local test_file=${3:?Missing Test File}
echo "${test_func}" "${test_file}"
while [ $(((cnt -= 1 ) )) != 0 ]; do
"${test_func}" "${test_file}"
done
}
: ${TMPDIR:=/tmp}
setup_test(){
touch "${TMPDIR}/file"
touch "${TMPDIR}/exec_file"
chmod a+x "${TMPDIR}/exec_file"
time test_case 10000 test_file_1 "${TMPDIR}/file"
time test_case 10000 test_file_2 "${TMPDIR}/file"
time test_case 10000 test_file_1 "${TMPDIR}/exec_file"
time test_case 10000 test_file_2 "${TMPDIR}/exec_file"
}
~/ks_qnx/test_bash.sh setup_test
test_file_1 /tmp/file
real 0m0.132s
user 0m0.128s
sys 0m0.004s
test_file_2 /tmp/file
real 0m0.148s
user 0m0.116s
sys 0m0.028s
test_file_1 /tmp/exec_file
real 0m0.138s
user 0m0.128s
sys 0m0.008s
test_file_2 /tmp/exec_file
real 0m0.153s
user 0m0.128s
sys 0m0.024s
Am 23.08.2017 um 16:27 schrieb Chet Ramey:
On 8/23/17 10:24 AM, Dethrophes wrote:
Which I always understood as the correct way of doing this in the
first place...
It's not as good as multiple test commands: test -f file && test -x
file.
There's no ambiguity and you get short-circuiting.
Only if you are using the test built-in, otherwise the latter means 2
spawns/forks however the shell in question calls the test exec.
Since bash has a test builtin, this isn't exactly on point. But you have
to accept this kind of micro-inefficiency with a shell that sacrifices
speed for size.