linux python 更快,为什么python hashlib.md5比linux更快coreutils md5sum

蟒蛇hashlib

def get_hash(fpath, algorithm='md5', block=32768):

if not hasattr(hashlib, algorithm):

return ''

m = getattr(hashlib, algorithm)()

if not os.path.isfile(fpath):

return ''

with open(fpath, 'r') as f:

while True:

data = f.read(block)

if not data:

break

m.update(data)

return m.hexdigest()

的coreutils MD5SUM

def shell_hash(fpath, method='md5sum'):

if not os.path.isfile(fpath):

return ''

cmd = [method, fpath] #delete shlex

p = Popen(cmd, stdout=PIPE)

output, _ = p.communicate()

if p.returncode:

return ''

output = output.split()

return output[0]

大约有我的测试结果计算MD5和SHA1时间4列。

第1列是coreutils md5sum或sha1sum的校准时间。

第2列是python hashlib md5或sha1的校准时间,通过读取大块。

第3列是python hashlib md5或sha1的校准时间,通过读取块。

第4列是python hashlib md5或sha1的校准时间,通过读取 chunk。

4.08805298805 3.81827783585 3.72585606575 5.72505903244

6.28456497192 3.69725108147 3.59885907173 5.69266486168

4.08003306389 3.82310700417 3.74562311172 5.74706888199

6.25473690033 3.70099711418 3.60972714424 5.70108985901

4.07995700836 3.83335709572 3.74854302406 5.74988412857

6.26068210602 3.72050404549 3.60864400864 5.69080018997

4.08979201317 3.83872914314 3.75350999832 5.79242300987

6.28977203369 3.69586396217 3.60469412804 5.68853116035

4.0824379921 3.83340883255 3.74298214912 5.73846316338

6.27566385269 3.6986720562 3.6079480648 5.68188500404

4.10092496872 3.82357311249 3.73044300079 5.7778570652

6.25675201416 3.78636980057 3.62911510468 5.71392583847

4.09579920769 3.83730792999 3.73345088959 5.73320293427

6.26580905914 3.69428491592 3.61320495605 5.69155502319

4.09030103683 3.82516098022 3.73244214058 5.72749185562

6.26151800156 3.6951239109 3.60320997238 5.70400810242

4.07977604866 3.81951498985 3.73287010193 5.73037815094

6.26691818237 3.72077894211 3.60203289986 5.71795105934

4.08536100388 3.83897590637 3.73681998253 5.73614501953

6.2943251133 3.72131896019 3.61498594284 5.69963502884

(My computer has 4-core i3-2120 CPU @ 3.30GHz, 4G memory.

The file calculated by these program is about 2G in size.

The odd rows are about md5 and the even rows are about sha1.

The time in this table are in second.)

经过100多次测试,我发现python hashlib总是比md5sum或sha1sum快。

我还阅读了有关Python2.7/Modules/{md5.c,md5.h,md5module.c}和gnulib lib/{md5.c,md5.h}的源代码中的一些文档。它们都是MD5(RFC 1321)的实现。

在gnulib中,md5块被读取32768。

我对md5和C源代码了解不多。有人能帮我解释这些结果吗?

为什么我想问这个问题的另一个原因是,许多人认为md5sum比python_hashlib更快,因为在写入python代码时他们更喜欢使用md5sum。但这似乎是错误的。

2014-03-25

Feng Liu

+2

那里有很多移动部件。您可能正在测量第二个函数中的“POpen.communicate”的性能。尝试使用'time'在本地bash中计时。 –

+2

不完全的答案:不同的实现之间通常会有性能差异,所以你不会期望Python和Core Utils必须是相同的速度。但是,您的方法中缺少一些内容,例如您是否首先预热高速缓存,以及结果的差异。至于代码,因为'shlex.split()'和''%s%s'%()'代替''md5sum',path]'而不是'shlex.split' x,y)'最多只是做相反的事情。最糟糕的是,'shlex.split()'会破坏参数。 –

+0

谢谢。我已经删除了'shlex'。我没有期待Python和coreutils以相同的速度实现md5。我会阅读代码来找出原因。 –

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值