カテゴリ
全体プログラミング scheme verilog 未分類 以前の記事
2016年 04月2016年 03月 2016年 02月 2016年 01月 2015年 12月 2015年 11月 2015年 10月 2015年 09月 2015年 08月 2015年 07月 2015年 06月 2015年 03月 お気に入りブログ
PHPで競技プログラミングメモ帳
最新のトラックバック
ライフログ
検索
タグ
racket
その他のジャンル
ブログパーツ
最新の記事
外部リンク
ファン
記事ランキング
ブログジャンル
画像一覧
|
テキスト処理の性能評価のため、10行程度のテキストファイルを10000回繰り返して表示させたいと思いました。catやsedでできるかなと思いましたが、そのようなオプションはなさげ。
ただ、which repeatと入力すると、そういうコマンドがあるっぽい。 $ which repeat repeat: shell reserved word (※もともとcsh系のコマンドらしく、bashでは使えません。zshでは使えます) さっそく使ってみることに。 $ cat AtoZ A (略) Z $ wc AtoZ 26 26 52 AtoZ $ repeat 10 cat AtoZ > tmp $ wc tmp 26 26 52 tmp おかしいなぁ もしかしてtmpが10回ファイルオープンしているとか $ (repeat 10 cat AtoZ) > tmp $ wc tmp 260 260 520 tmp うまくいきました。 ところが、この時点で、なんか動作がもっさりしています。なので $ time (repeat 10 cat AtoZ) > tmp ( repeat 10; do; cat AtoZ; done; ) > tmp 0.04s user 0.01s system 70% cpu 0.085 total $ time (repeat 1000 cat AtoZ) > tmp ( repeat 1000; do; cat AtoZ; done; ) > tmp 1.48s user 3.86s system 80% cpu 6.638 total 1000回の繰り返しで6.6秒もかかっています(Cygwinでの実行結果です)。 しばらく考えて、繰り返した回数だけcatのプロセスを生成していることに気が付きました。根本的すぎて嫌になります。 catに代わる繰り返し出力用コマンドを作ってみました。 $ cat dog #!/usr/bin/python # dog command --- concatnate standard input repeatedly # cat [file] | dog [num] import sys text = sys.stdin.read() for i in range(int(sys.argv[1])): print text, dogを実行した結果、物凄く早く終わりました。 $ time (cat AtoZ | dog 1000) > tmp ( cat AtoZ | dog 1000; ) > tmp 0.03s user 0.01s system 78% cpu 0.058 total dogでは標準入力しか受け付けず、しかもプログラム内のバッファに一度ためてから出力するので、長大な標準入力は受け付けられません。長大なファイルを入力する場合はプロセス生成時間よりもファイルアクセス時間のほうが長いのでrepeatを使ってくださいということです。
by tempurature
| 2015-09-12 22:57
| プログラミング
| ||||||||||||||||||||||
ファン申請 |
||