なぜ「detex」を含む「無駄な猫を使う」が代替品より10倍速いのですか?

なぜ「detex」を含む「無駄な猫を使う」が代替品より10倍速いのですか?

LaTeXファイルの単語数を数え、次のコマンドを視覚的に使用しました。

$ cat *tex | detex | wc -w

それから私はすぐにその人に気づきました。役に立たない使用cat、次のコードを使用してジョブを再実行しました。

$ detex *tex | wc -w

追加のプロセスをしなかったことが誇りに思って、使用していないので確認してみたかったのですが、catこのバージョンを見てびっくりしました。そして cat実際には10倍速いです:

$ time cat *tex | detex | wc -w
    5000

real    0m0.013s
user    0m0.005s
sys 0m0.007s
$ time detex *tex | wc -w
    5000

real    0m0.144s
user    0m0.124s
sys 0m0.014s

このように速度差が出る理由は何でしょうか?catディスクからファイルを読むよりもディスクからファイルを読み取る方がはるかに高速ですかdetex


ベンチマーク用に5つのサンプル「LaTeX」ファイルを作成しました。

$ for i in {1..5}; do shuf -n 1000 /usr/share/dict/words > "file${i}.tex"; done

ベストアンサー1

予備結果は一貫性がないようです。

detex最新バージョンがインストールされました自分で作った:

$ detex -v

OpenDetex version 2.8.9
https://github.com/pkubowicz/opendetex

サンプルLaTeXファイルを入手してください:

$ curl https://raw.githubusercontent.com/latex3/latex3/main/articles/2011-current-state.tex > file.tex

ファイルのコピーを100個作成しました。

$ for i in {1..100}; do cp file.tex "file${i}.tex"; done

このcatバージョンを5回実行してください。

$ for i in {1..5}; do time cat *tex | detex | wc -w; done
   78174

real    0m0.024s
user    0m0.025s
sys 0m0.012s
   78174

real    0m0.021s
user    0m0.023s
sys 0m0.009s
   78174

real    0m0.019s
user    0m0.020s
sys 0m0.008s
   78174

real    0m0.019s
user    0m0.019s
sys 0m0.008s
   78174

real    0m0.017s
user    0m0.018s
sys 0m0.008s

catバージョンを5回実行してください。

$ for i in {1..5}; do time detex *tex | wc -w; done
   78174

real    0m0.027s
user    0m0.023s
sys 0m0.009s
   78174

real    0m0.024s
user    0m0.021s
sys 0m0.007s
   78174

real    0m0.021s
user    0m0.020s
sys 0m0.005s
   78174

real    0m0.019s
user    0m0.017s
sys 0m0.005s
   78174

real    0m0.018s
user    0m0.016s
sys 0m0.005s

おすすめ記事