カメニッキ

カメとインコと釣りの人です

大きなテキストファイルをawkで処理するときにcatで投げ込むのと、ファイル読み込みするのどっちが速いか比較




2.5GBvimvimsublime textsublime textawk
$ cat sample.txt | awk '$5 ~ /((26|27|28|29|30)\/Jun|(01|02|03)\/Jul)/{ print }' > result.txt




cat

MATSUMOTO, Ryosuke (@matsumotory) | Twitter  











(一)awkver

(二)catver


 
$ time awk '$5 ~ /((26|27|28|29|30)\/Jun|(01|02|03)\/Jul)/{ print }' sample.txt > result1.txt

Twitter

@matsumotory あ、time コマンドが cat にしか効いてない...

 Shigeki Morimoto (@shmorimo) July 22, 2015



@tap1ra time sh -c "command" みたいにすればtimeで測れますね

  (@buty4649) July 22, 2015


# $ 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


cat


awkcat - ablog