On 03/25/2015 11:39 PM, Peng Yu wrote: > I find that "-exec test ..." can be slower than "-exec $(which test) > ...". Is possible that `find` internally use "which", so that users > don't need to explicitly call it? Thanks.
How much slower is it for you? In my test case, the findutils repository which currently contains 13334 files, the command $ find . -exec /usr/bin/test '{}' \; takes ~6.5s while without the absolute path it needs ~6.6 - ~6.7 seconds. In my case, "/usr/bin" is the 3rd entry in $PATH. When moving "/usr/bin" to the first position in PATH, the time is about equal to that with the absolute path. This means the overhead of fork/execvp is much greater than searching for the program in execvp. If I add more entries in PATH before "/usr/bin", then the time increases, as expected. You can play with different values for N: ( N=50; \ export PATH="$(yes $HOME/bin | head -n $N | paste -s -d:):/usr/bin"; \ for i in 1 2 3; do \ for f in test /usr/bin/test; do \ echo "=== $f ==="; time find . -exec $f '{}' \; ; \ done ; \ done ) Of course the situation gets worse when a directory on a remote files system is in PATH before "/usr/bin". Regarding 'internally use "which"': find does not do anything special and instead relies on execvp() to execute the program. And I think this is exactly what is specified and expected. The example seems to be a bit contrived, but there may even be scripts relying on running different programs in the invocation for each matched file. Have a nice day, Berny