我们经常在 ~/.zshrc
塞一堆自定义函数和 alsias、export 各种 path,导致 zsh 的启动时间长的让人难以忍受。
ZSH 提供了一个自动的性能分析工具 zsh/zprof
用于分析 zsh 启动时各组件或者说各脚本执行所占用的时间,使用方法如下:
编辑 ~/.zshrc
在配置文件首行插入 zmodload zsh/zprof
,然后在最后一行插入 zprof
。
~/.zshrc1 2 3
| zmodload zsh/zprof ... zprof
|
接着运行下面的命令来查看 zsh 启动时各组件占用的时间
示例结果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
| > time zsh -i -c exit num calls time self name ----------------------------------------------------------------------------------- 1) 1 29.04 29.04 30.43% 29.04 29.04 30.43% handle_completion_insecurities 2) 2 56.19 28.10 58.87% 28.38 14.19 29.74% compinit 3) 2 27.81 13.90 29.13% 27.81 13.90 29.13% compaudit 4) 2 8.18 4.09 8.57% 8.18 4.09 8.57% (anon) 5) 1 0.76 0.76 0.80% 0.76 0.76 0.80% colors 6) 3 0.40 0.13 0.42% 0.40 0.13 0.42% add-zsh-hook 7) 1 0.23 0.23 0.25% 0.23 0.23 0.25% is-at-least 8) 3 0.20 0.07 0.21% 0.20 0.07 0.21% compdef 9) 2 0.18 0.09 0.19% 0.18 0.09 0.19% wrap_clipboard_widgets 10) 5 0.17 0.03 0.18% 0.17 0.03 0.18% is_plugin 11) 1 0.04 0.04 0.04% 0.04 0.04 0.04% detect-clipboard 12) 2 0.03 0.01 0.03% 0.03 0.01 0.03% env_default 13) 1 0.02 0.02 0.02% 0.02 0.02 0.02% bashcompinit
-----------------------------------------------------------------------------------
2) 2 56.19 28.10 58.87% 28.38 14.19 29.74% compinit 1/2 27.81 27.81 29.13% 0.30 0.30 compaudit [3]
-----------------------------------------------------------------------------------
1) 1 29.04 29.04 30.43% 29.04 29.04 30.43% handle_completion_insecurities
-----------------------------------------------------------------------------------
1/2 27.81 27.81 29.13% 0.30 0.30 compinit [2] 1/2 27.51 27.51 28.82% 27.51 27.51 compaudit [3] 3) 2 27.81 13.90 29.13% 27.81 13.90 29.13% compaudit 1/2 27.51 27.51 28.82% 27.51 27.51 compaudit [3]
-----------------------------------------------------------------------------------
4) 2 8.18 4.09 8.57% 8.18 4.09 8.57% (anon)
-----------------------------------------------------------------------------------
5) 1 0.76 0.76 0.80% 0.76 0.76 0.80% colors
-----------------------------------------------------------------------------------
6) 3 0.40 0.13 0.42% 0.40 0.13 0.42% add-zsh-hook
-----------------------------------------------------------------------------------
7) 1 0.23 0.23 0.25% 0.23 0.23 0.25% is-at-least
-----------------------------------------------------------------------------------
8) 3 0.20 0.07 0.21% 0.20 0.07 0.21% compdef
-----------------------------------------------------------------------------------
9) 2 0.18 0.09 0.19% 0.18 0.09 0.19% wrap_clipboard_widgets
-----------------------------------------------------------------------------------
10) 5 0.17 0.03 0.18% 0.17 0.03 0.18% is_plugin
-----------------------------------------------------------------------------------
11) 1 0.04 0.04 0.04% 0.04 0.04 0.04% detect-clipboard
-----------------------------------------------------------------------------------
12) 2 0.03 0.01 0.03% 0.03 0.01 0.03% env_default
-----------------------------------------------------------------------------------
13) 1 0.02 0.02 0.02% 0.02 0.02 0.02% bashcompinit zsh -i -c exit 0.10s user 0.06s system 95% cpu 0.165 total
|
启动时间只需 0.1s,好耶,这源于我替换了 nvm 为 fnm 的结果!
参考资料
- Profiling zsh startup time