[PATCHv3 16/16] bash prompt: mention that PROMPT_COMMAND mode is faster
- From
SZEDER Gábor <szeder@ira.uka.de>
- Date
- Jun 24, 2013, 16:39 UTC
- Message-ID
- <1372091966-19315-17-git-send-email-szeder@ira.uka.de>
- In-Reply-To
- <1372091966-19315-1-git-send-email-szeder@ira.uka.de>
__git_ps1() is usually added to the prompt inside a command substitution, imposing the overhead of fork()ing a subshell. Using __git_ps1() for $PROMPT_COMMAND is slightly faster, because it avoids that command substitution.
Mention this in the comments about setting up the git prompt.
The whole series speeds up the bash prompt on Windows/MSysGit considerably. Here are some timing results in three scenarios, each repeated 10 times:
At the top of the work tree, before:
$ time for i in {0..9} ; do prompt="$(__git_ps1)" ; done real 0m1.716s
user 0m0.301s
sys 0m0.772sAfter:
real 0m0.687s
user 0m0.075s
sys 0m0.396sAfter, from $PROMPT_COMMAND:
$ time for i in {0..9} ; do __git_ps1 '\h:\w' '$ ' ; done real 0m0.546s
user 0m0.075s
sys 0m0.181sAt the top of the work tree, detached head, before:
real 0m2.574s
user 0m0.376s
sys 0m1.207sAfter:
real 0m1.139s
user 0m0.151s
sys 0m0.500sAfter, from $PROMPT_COMMAND:
real 0m1.030s
user 0m0.245s
sys 0m0.336sIn a subdirectory, during rebase, stash status indicator enabled, before:
real 0m3.557s
user 0m0.495s
sys 0m1.767sAfter:
real 0m0.717s
user 0m0.120s
sys 0m0.300sAfter, from $PROMPT_COMMAND:
real 0m0.577s
user 0m0.047s
sys 0m0.258sOn Linux the speedup ratio is comparable to Windows, but overall it was about an order of magnitude faster to begin with. The last case from above, repeated 100 times, before:
$ time for i in {0..99} ; do prompt="$(__git_ps1)" ; done real 0m2.806s
user 0m0.180s
sys 0m0.264sAfter:
real 0m0.857s
user 0m0.020s
sys 0m0.028sSigned-off-by: SZEDER Gábor <szeder@ira.uka.de> --- contrib/completion/git-prompt.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/contrib/completion/git-prompt.sh b/contrib/completion/git-prompt.sh index 7152ae49..daed6a1d 100644 --- a/contrib/completion/git-prompt.sh +++ b/contrib/completion/git-prompt.sh @@ -15,11 +15,11 @@ # Bash: PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ ' # ZSH: PS1='[%n@%m %c$(__git_ps1 " (%s)")]\$ ' # the optional argument will be used as format string. -# 3b) Alternatively, if you are using bash, __git_ps1 can be -# used for PROMPT_COMMAND with two parameters, <pre> and -# <post>, which are strings you would put in $PS1 before -# and after the status string generated by the git-prompt -# machinery. e.g. +# 3b) Alternatively, for a slighly faster prompt, if you are +# using bash, __git_ps1 can be used for PROMPT_COMMAND +# with two parameters, <pre> and <post>, which are strings +# you would put in $PS1 before and after the status string +# generated by the git-prompt machinery. e.g. # Bash: PROMPT_COMMAND='__git_ps1 "\u@\h:\w" "\\\$ "' # ZSH: precmd () { __git_ps1 "%n" ":%~$ " "|%s" } # will show username, at-sign, host, colon, cwd, then
-- 1.8.3.1.599.g4459181