2017-08-10 14 views
0

私は別の方法でフォルダを圧縮しようとしました。私の理解は、Pythonの組み込みモジュールが常にsubprocess.call( "Linuxコマンド")より高速です。 しかし、私はちょうどいくつかのデモを行った。 tarfileモジュールは、subprocess.call( "tar")よりも遅いです。PythonのtarファイルがLinuxのコマンドよりも遅い

#!/usr/bin/python 

import os 
import time 
import tarfile 
import subprocess 

tStart1 = time.time() 

TestFolder = ["Jack", "Robin"] 
for folder in TestFolder: 
    name = "/mnt/ShareDrive/Share/ExistingUsers/"+folder 
    path = "/mnt/TEST2/" 
    tar = tarfile.open(path+folder+".tar.gz", "w:gz") 
    tar.add(name) 
    tar.close() 
tEnd1 = time.time() 

time.sleep(2) 

tStart2 = time.time() 
for folder in TestFolder: 
    path = "/mnt/TEST1/" 
    subprocess.call(["tar", "zcvf", path+folder+".tar.gz", "-P", "/mnt/ShareDrive/Share/ExistingUsers/"+folder]) 
tEnd2 = time.time() 

print "The module cost %f sec" % (tEnd1 - tStart1) 
print "The subprocess cost %f sec" % (tEnd2 - tStart2) 

tarfileモジュールのコストは63秒です。 サブプロセスコストはわずか32秒です。

2つのフォルダの合計サイズが433メガバイト

+0

'私の理解は、Pythonの組み込みモジュールが常にsubprocess.call( "Linuxコマンド")より速いということです。その前提の根拠はありません。 –

+0

@PauloAlmeida私を訂正していただきありがとうございます。 – Ilikeperl

答えて

2

tarではtarfileモジュールが処理タールの純粋なPython実装であるCで書かれています。モジュールがコマンドより速くなる方法はありません。

+0

説明をありがとう。私はPythonモジュールに大きな誤解があるようです。どうもありがとうございます! – Ilikeperl

関連する問題