Last active
July 26, 2017 02:47
-
-
Save wfxr/c061d54266491f42507383bbc5688488 to your computer and use it in GitHub Desktop.
zsh和bash测试命令是否存在
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bash | |
The testing results for bash is shown in the following table, with each testing command running for 100,000 times: | |
TESTING COMMAND RUNNING TIME (REAL, USER, SYS) | |
type ls &>/dev/null 0m1.476s, 0m0.632s, 0m0.836s | |
hash ls &>/dev/null 0m1.598s, 0m0.740s, 0m0.856s | |
command -v ls &>/dev/null 0m1.441s, 0m0.660s, 0m0.776s | |
which ls &>/dev/null 2m0.418s, 0m3.852s, 0m13.212s | |
According to the results, type and command -v are the two fastest ways to test the existence of a command in bash. I have also run the tests with either set +h or set -h, but the results do not have obvious difference. | |
Zsh | |
The testing results for bash is shown in the following table, with each testing command running for 2,000,000 times: | |
TESTING COMMAND RUNNING TIME (REAL, USER, SYS) | |
type ls &>/dev/null 0m8.29s, 0m3.48s, 0m4.80s | |
hash ls &>/dev/null 0m5.92s, 0m2.49s, 0m3.42s | |
command -v ls &>/dev/null 0m8.85s, 0m3.61s, 0m5.18s | |
which ls &>/dev/null 0m8.24s, 0m3.18s, 0m5.00s | |
(( $+commands[ls] )) 0m4.01s, 0m3.99s, 0m0.00s | |
According to the results, (( $+commands[ls] )) is a clear winner in zsh. | |
Ref:https://www.topbug.net/blog/2016/10/11/speed-test-check-the-existence-of-a-command-in-bash-and-zsh/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment