LoginSignup
3
1

More than 5 years have passed since last update.

grep,sed,awkの処理速度比較

Posted at

月並みといった内容ですがご容赦下さい。

temp.txtからキーワード"e5"が含まれる行をカウントすることで、grep,sed,awkの処理速度を比較します。

temp.txtの内容

$ head temp.txt 
 37 7a bc af 27 1c 00 02 75 be 77 96 55 e5 9f 54
 00 00 00 00 27 00 00 00 00 00 00 00 b3 f4 0c 81
 00 44 94 05 c4 7a 27 f6 f7 ee 89 8e 50 90 88 b3
 aa d5 50 2b fc 92 c0 20 97 69 80 b6 b6 4c bd 59
 c4 1c 86 45 68 d3 8d e1 61 b1 2a df 0b 0a 12 39
 4a 80 fe 79 e1 38 9f 4f 7e b9 af f9 9d c7 57 90
 0b 2c 2d 23 96 f7 7e 15 8a 48 df 00 ef 01 ea 56
 1e 8f f4 a3 63 d8 f3 a2 ea 21 4f 6d d4 63 b9 b9
 e8 8d b6 e2 4b 6d 32 7a cf b1 6f b5 2e b5 af c1
 00 9d a2 0b 9a e6 d3 43 42 56 d8 07 a2 cb b0 6b
...以下省略

temp.txtのサイズ

$ ls -lh temp.txt 
-rw-r--r-- 1 hoge hoge 2.4G  5月  2 01:20 temp.txt

temp.txtの行数

$ wc -l temp.txt
52381654 temp.txt

実行結果

$ time grep e5 temp.txt | wc -l
3180957

real    0m4.185s
user    0m3.536s
sys 0m0.812s
$ time sed -n /e5/p temp.txt | wc -l
3180957

real    0m19.568s
user    0m18.748s
sys 0m0.968s
$ time awk '/e5/{print}' temp.txt | wc -l
3180957

real    0m12.740s
user    0m11.728s
sys 0m1.152s

grepが最速でした。ちなみにwcの代替手段としてawkを使う場合は次の方法でできるようでした。

$ time grep e5 temp.txt | awk 'END{print NR}'
3180957

real    0m4.197s
user    0m3.764s
sys 0m0.820s
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1