大きなテキストファイルをawkで処理するときにcatで投げ込むのと、ファイル読み込みするのどっちが速いか比較
追記が増えたので整理
— Shigeki Morimoto (@shmorimo) July 22, 2015
すると、、
— ぶてい (@buty4649) July 22, 2015
と回避策を教えていただいたので、再度計測しました。
経緯
2.5GBのテキストファイルを加工する必要があり、①vimで開いて加工→vim死亡②sublime textで開いて加工→sublime text死亡となったため、awkを用いて以下の様なコマンドを実行した。$ cat sample.txt | awk '$5 ~ /((26|27|28|29|30)\/Jun|(01|02|03)\/Jul)/{ print }' > result.txtすると ●﹁catいらなくね?﹂と指摘 ●さらにMATSUMOTO, Ryosuke (@matsumotory) | Twitter < ﹁キャッシュに入れて高速化してるんかと思った﹂ とコメントをもらいました。ので、どっちが速いかの検証です。
注意
●加工の目的はログファイルからある期間だけの行を抜き取りたい ●正規表現がいけてないのは気にしない比較
(一)awkにファイル指定するver (二)catで投げ込むver ※ 以下のような方法で計測していたところ、、、、$ time awk '$5 ~ /((26|27|28|29|30)\/Jun|(01|02|03)\/Jul)/{ print }' sample.txt > result1.txtTwitterにてご指摘をいただきました。
@matsumotory あ、time コマンドが cat にしか効いてない...
@tap1ra time sh -c "command" みたいにすればtimeで測れますね
# $ time shに渡すときシングルクォーテーションのエスケープ方法がわからず、ファイルにした # $ time command1.shでも良かったのかも。 [root@localhost sample]# cat command1 awk '$5 ~ /((26|27|28|29|30)\/Jun|(01|02|03)\/Jul)/{ print }' sample.txt > result1.txt # キャッシュ削除処理 [root@localhost sample]# echo1> /proc/sys/vm/drop_caches;echo2> /proc/sys/vm/drop_caches;echo3> /proc/sys/vm/drop_caches [root@localhost sample]# free total used free shared buffers cached Mem: 502160 53188 448972 468 500 7716 -/+ buffers/cache: 44972 457188 Swap: 1015804 0 1015804 [root@localhost sample]# time sh command1 real 0m53.926s user 0m0.926s sys 0m33.158s
[root@localhost sample]# cat command2 cat sample.txt | awk '$5 ~ /((26|27|28|29|30)\/Jun|(01|02|03)\/Jul)/{ print }' > result2.txt [root@localhost sample]# echo1> /proc/sys/vm/drop_caches;echo2> /proc/sys/vm/drop_caches;echo3> /proc/sys/vm/drop_caches [root@localhost sample]# free total used free shared buffers cached Mem: 502160 52964 449196 468 236 7716 -/+ buffers/cache: 45012 457148 Swap: 1015804 0 1015804 [root@localhost sample]# time sh command2 real 0m30.789s user 0m2.644s sys 0m22.277s
# できあがったブツに差異はなさそう [root@localhost sample]# md5sum result1.txt ff6a7bf31b5ba8bdbba619258c0c4285 result1.txt [root@localhost sample]# md5sum result2.txt ff6a7bf31b5ba8bdbba619258c0c4285 result2.txt