我測試過md5,sha1,crc32的速度,結果顯示md5比sha1慢,我對它感到困惑,因爲sha1比md5更復雜。爲什麼md5比py1慢sha1 2.7.11
我執行我的測試Python腳本哈希PNG圖像(你可以使用圖像任何你想要的)10000次,如: crc_vs_md5.py C:\用戶\ bigwalnut \桌面\ def.png 10000
結果(單位:s): MD5:0.122737341241 SHA1:0.0984247229759 CRC32:0.0577822398549
下面是代碼:
from hashlib import md5, sha1
from zlib import crc32
import sys
import time
numberoftime = 0
content = None
def getMd5():
for x in range(0, numberoftime):
mdfive = md5()
mdfive.update(content)
mdfive.hexdigest()
def getSha1():
for x in range(0, numberoftime):
sha1Obj = sha1()
sha1Obj.update(content)
sha1Obj.hexdigest()
def getCrc32():
for x in range(0, numberoftime):
crc32(content) & 0xffffffff
if len(sys.argv) < 3:
print('You must enter: filename, numberoftime')
exit(1)
elif len(sys.argv) > 3:
print('Only filename, numberoftime is permitted')
exit(1)
filename = sys.argv[1]
numberoftime = int(sys.argv[2])
print filename, numberoftime
with open(filename, 'rb') as f:
content = f.read()
start = time.clock()
getMd5()
stop = time.clock()
print "md5: ", stop - start
start = time.clock()
getSha1()
stop = time.clock()
print "sha1: ", stop - start
start = time.clock()
getCrc32()
stop = time.clock()
print "crc32: ", stop - start
[此鏈接上的結果](http://atodorov.org/blog/2013/02/05/performance-test-md5-sha1-sha256-sha512/)與您的衝突。 – Selcuk
瞭解[timeit](https://docs.python.org/2/library/timeit.html)以獲得更準確結果的好時機。 – Kupiakos
@Selcuk,對不起,我寫了一個錯誤的標題,在我的測試中,md5比sha1慢。 –