Skip to content

Commit 06314be

Browse files
committed
feat: add benchmark for displaying all programs in PATH
Summary: This is the first regression we've seen for `fd` vs. `find`. ```sh $ PATH="/opt/homebrew/Cellar/findutils/4.10.0/libexec/gnubin/:$PATH" ./warm-cache-exe-paths.sh Benchmark 1: FIND_PROG=find FIND_ARGS='-maxdepth 1 -executable -type f,l -printf %f\n' get_programs_in_path Time (mean ± σ): 142.0 ms ± 6.0 ms [User: 32.7 ms, System: 72.0 ms] Range (min … max): 129.7 ms … 154.6 ms 20 runs Benchmark 2: FIND_PROG='fd .' FIND_ARGS='--hidden --max-depth=1 --type=executable --follow --format {/} ' get_programs_in_path Time (mean ± σ): 272.1 ms ± 6.0 ms [User: 88.8 ms, System: 142.1 ms] Range (min … max): 258.2 ms … 279.9 ms 10 runs Summary FIND_PROG=find FIND_ARGS='-maxdepth 1 -executable -type f,l -printf %f\n' get_programs_in_path ran 1.92 ± 0.09 times faster than FIND_PROG='fd .' FIND_ARGS='--hidden --max-depth=1 --type=executable --follow --format {/} ' get_programs_in_path Both fd and find found the same 2943 results ```
1 parent 5738954 commit 06314be

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

warm-cache-exe-paths.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
3+
source "prelude.sh"
4+
5+
# extracted from https://github.com/vegerot/dotfiles/blob/a9a50230d808572173d3eeec057739d0fe8d4470/bin/fzf-menu by @vegerot
6+
get_programs_in_path() {
7+
local find_prog
8+
local find_args
9+
if [[ -n "${FIND_PROG:-}" ]]; then
10+
find_prog=$FIND_PROG
11+
find_args=$FIND_ARGS
12+
elif $(find / -maxdepth 0 -executable &>/dev/null) || command -v gfind &>/dev/null; then
13+
# GNU find
14+
find_prog=$(command -v gfind || echo find)
15+
find_args="-maxdepth 1 -executable -printf %f\n"
16+
elif command -v fd > /dev/null; then
17+
find_prog="fd ."
18+
find_args="--max-depth=1 --type=executable --exec basename {} ;"
19+
else
20+
# BSD find
21+
find_prog=find
22+
find_args="-maxdepth 1 -perm +111 -type f,l -exec basename {} ;"
23+
fi
24+
25+
local pathDeduped=$(printf '%s\n' $PATH | tr ':' '\n' | uniq )
26+
for p in $pathDeduped; do
27+
$find_prog $p $find_args 2>/dev/null || true
28+
done \
29+
| awk '!x[$0]++'
30+
# awk removes duplicates without sorting. Thanks https://stackoverflow.com/a/11532197/6100005 \
31+
}
32+
export -f get_programs_in_path
33+
34+
35+
COMMAND_FIND="FIND_PROG=find FIND_ARGS='-maxdepth 1 -executable -type f,l -printf %f\n' get_programs_in_path"
36+
COMMAND_FD="FIND_PROG='fd .' FIND_ARGS='--hidden --max-depth=1 --type=executable --follow --format {/} ' get_programs_in_path"
37+
38+
hyperfine --warmup "$WARMUP_COUNT" \
39+
"$COMMAND_FIND" \
40+
"$COMMAND_FD" \
41+
--export-markdown results-warm-cache-no-pattern.md
42+
check_for_differences "false" "$COMMAND_FIND" "$COMMAND_FD"

0 commit comments

Comments
 (0)