5番目のsource.zshrcは、最初のsource.zshrcよりもはるかに時間がかかります。

5番目のsource.zshrcは、最初のsource.zshrcよりもはるかに時間がかかります。

昨日、私は現在zshシェルを更新するために.zshrcを取得するために必要なシェルスクリプトを書いていました。

ソースコード処理を続けてみると時間がかかるようですsource .zshrc

sourceまたは、文書は.次のようになります。

. file [ arg ... ]
       Read commands from file and execute them in the current shell environment.

       If file does not contain a slash, or if PATH_DIRS is set, the shell looks in the components of $path to find the directory containing file.  Files in the current  directory
       are  not  read unless `.' appears somewhere in $path.  If a file named `file.zwc' is found, is newer than file, and is the compiled form (created with the zcompile builtin)
       of file, then commands are read from that file instead of file.

       If any arguments arg are given, they become the positional parameters; the old positional parameters are restored when the file is done executing.  However, if no arguments
       are given, the positional parameters remain those of the calling context, and no restoring is done.

       If  file  was  not found the return status is 127; if file was found but contained a syntax error the return status is 126; else the return status is the exit status of the
       last command executed.
source file [ arg ... ]
       Same as `.', except that the current directory is always searched and is always searched first, before directories in $path.

もしそうなら、質問は、なぜ最後のソースが開始よりもはるかに長くかかるのかということです。

ベストアンサー1

source(cshから)または(Bourneシェルから)は、シェルを要求し、引数として渡されたファイルに格納されているシェルコードを解釈する.シェル組み込みコマンドです。read

実行にかかる時間は、そのコードを読み取って解釈するのにかかる時間なので、すべてはそのコードが実行する操作に依存します。

たとえば、このコードに次のものが含まれているとします。

var=$var$var

変数のサイズは実行されるたびに2倍に増加し$var、このコードを実行すると、コピーする必要があるデータの量が2倍になるため、実行に時間がかかると想像できます。あなたがそれを実行する時間。

最も時間がかかるコマンドを見つけるには、次のようにします。

PS4='%D{%T.%3.} %N:%i> '; set -o xtrace; source ~/.zshrc

これにより、実行中のすべてのコマンドがミリ秒単位の精度でタイムスタンプで印刷されます。

おすすめ記事