ファイルと結合 + bzip2 の最速の実行手段を試すべく、以下の二つを比較してみた
- 外部コマンド(/bin/cat, /bin/bzip2) で実行した場合
- pure python で実行した場合
タブ区切りのテキストが数十万行入ったテキストファイルを6つ持ってきて、それを結合して bzip2 する、というシナリオを使う。
尚、対象となるファイルは以下の通り。同じものはない。
$ ls -larth test_*
-rw-rw-r-- 1 mumumu mumumu 98M 3月 20 15:52 test_c4d5f3050e2348bf82b403c7c42b348f.txt
-rw-rw-r-- 1 mumumu mumumu 23M 3月 20 15:52 test_96721d80548a4dc2b2307e10ca0257f2.txt
-rw-rw-r-- 1 mumumu mumumu 30M 3月 20 15:52 test_23cfd6d290dd45f88cb889e2d460b99a.txt
-rw-rw-r-- 1 mumumu mumumu 32M 3月 20 15:52 test_e24e9128e85e4a90a16e5ba3d58aa03d.txt
-rw-rw-r-- 1 mumumu mumumu 40M 3月 20 15:52 test_4d0aaa44baba4b35b41c9ba08c7a4f36.txt
-rw-rw-r-- 1 mumumu mumumu 39M 3月 20 15:52 test_dc78afd737504622a45240ad4abe45f3.txt
- /bin/cat, /bin/bzip2 を使ったコード
- https://gist.github.com/mumumu/9658232
- cat_and_bzip2_by_external_cmd.py
$ python cat_and_bzip2_by_external_cmd.py
## benchmarker: release 3.0.1 (for python)
## python platform: linux2 [GCC 4.7.3]
## python version: 2.7.4
## python executable: /home/mumumu/bigfile_bench/bin/python
## user sys total real
cat by /bin/cat 0.0000 0.0000 0.0000 0.1744
bz2 by /bin/bzip2 0.0000 0.0000 0.0000 30.2523
## Ranking real
cat by /bin/cat 0.1744 (100.0%) *************************
bz2 by /bin/bzip2 30.2523 ( 0.6%)
## Ratio Matrix real [01] [02]
[01] cat by /bin/cat 0.1744 100.0% 17347.1%
[02] bz2 by /bin/bzip2 30.2523 0.6% 100.0%
- pure python でファイルの結合とbzip2を行ったコード
- https://gist.github.com/mumumu/9658238
- cat_and_bzip2_by_pure_python.py
$ python cat_and_bzip2_by_pure_python.py
$ python cat_and_bzip2_by_pure_python.py
## benchmarker: release 3.0.1 (for python)
## python platform: linux2 [GCC 4.7.3]
## python version: 2.7.4
## python executable: /home/mumumu/bigfile_bench/bin/python
## user sys total real
cat by pure python 0.0200 0.1800 0.2000 0.1969
bz2 by bz2.BZ2File 29.6900 0.1400 29.8300 29.9140
## Ranking real
cat by pure python 0.1969 (100.0%) *************************
bz2 by bz2.BZ2File 29.9140 ( 0.7%)
## Ratio Matrix real [01] [02]
[01] cat by pure python 0.1969 100.0% 15191.7%
[02] bz2 by bz2.BZ2File 29.9140 0.7% 100.0%
cat についてはほぼ差はない。bzip2 については pure python の方が若干速い。何度か追試すると、bzip2 については1秒半近い差が見られた。