DEV Community

Benji πŸ™
Benji πŸ™

Posted on

TIL how to profile `zsh` start time

Disclaimer: I'm heavily referencing from this blog post

Tl:Dr: Package managers like nvm, pyenv etc are initialized on startup and if you don't need one of them then just remove them from zshrc.

Context

Everytime I load a new session on iterm I notice a significant wait time, so out of curiosity I googled around and discovered how to profile the ./zshrc file by using the below steps:

❯ time zsh -i -c exit zsh -i -c exit 0.87s user 1.13s system 42% cpu 4.685 total 
Enter fullscreen mode Exit fullscreen mode

The above command creates a temporary zsh subshell then measures the time it takes for the exit command to run within it. Finally, it prints the execution time to the console, which you see above is 4.6s

You can profile zsh by adding at the top zmodload zsh/zprof and zprof at the bottom. zprof is a built in profiler for zsh, and when executed I see the output similar to below:

num calls time self name ----------------------------------------------------------------------------------- 1) 6 2385.86 397.64 12.79% 2385.57 397.59 12.79% __conda_activate 2) 4 1959.50 489.88 10.50% 1959.50 489.88 10.50% git 3) 32 1940.77 60.65 10.40% 1940.77 60.65 10.40% mcfly_prompt_command 4) 6 1512.94 252.16 8.11% 1512.94 252.16 8.11% compdump 
Enter fullscreen mode Exit fullscreen mode

Your output might show something else, as my personal laptop shows nvm being the culprit, while my work laptop shows the above shows that conda is taking up most of the time. Conda is a package manager tool similar to pyenv, which I barely use and only had when experimenting with something months back.

Anyway, looking through my zshrc file I spot this to be the reason for the wait:

125 # >>> conda initialize >>> 126 # !! Contents within this block are managed by 'conda init' !! 127 __conda_setup="$('/Users/benji/miniconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)" 128 if [ $? -eq 0 ]; then 129 eval "$__conda_setup" 130 else 131 if [ -f "/Users/benji/miniconda3/etc/profile.d/conda.sh" ]; then 132 . "/Users/benji/miniconda3/etc/profile.d/conda.sh" 133 else 134 export PATH="/Users/benji/miniconda3/bin:$PATH" 135 fi 136 fi 137 unset __conda_setup 138 # <<< conda initialize <<< 
Enter fullscreen mode Exit fullscreen mode

As I'm fearful I'll need it again one day, I decide not to remove it and opted to comment it out instead. I then source ~/.zshrc and see that the start time was reduced from 4.6s to 0.9s:

❯ time zsh -i -c exit zsh -i -c exit 0.22s user 0.25s system 48% cpu 0.972 total 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)