简体   繁体   English

python sha1哈希计算的时间效率

[英]Time efficiency of python sha1 hash calculation

For these two ways of calculating a sha1 hash, regards with time efficiency , are they the same? 对于这两种计算sha1哈希的方式,考虑到时间效率 ,它们是一样的吗?

(1) Split the string to small chunks and update the hash multiple times (1)将字符串拆分为小块并多次更新哈希

import hashlib

...
...
sha1 = hashlib.sha1()
sha1.update(chunk1)
sha1.update(chunk2)
...

(2) Pass the complete string to the hash function and compute the hash only once (2)将完整的字符串传递给哈希函数,并且仅计算一次哈希

import hashlib
...
...
sha1 = hashlib.sha1()
sha1.update(the_complete_string)
...

There is additional overhead for each chunk: 每个块都有额外的开销:

  • you must split the string 您必须分割字符串
  • There are python calls into hashlib for each chunk 每个块都有对hashlib的python调用
  • the hash library must set up to handle each chunk 哈希库必须设置为处理每个块

So, there is overhead that scales with the number of chunks. 因此,开销随块数而定。 If you have a constant number of chunks, it probably doesn't matter. 如果您有恒定数量的块,则可能无关紧要。 However if you were to split a significant string into one-character chunks and update using each of those one-character chunks, the chunked approach would be significantly slower than the whole string approach. 但是,如果将重要的字符串拆分为一个字符的块并使用每个一个字符的块进行更新,则分块的方法将比整个字符串的方法慢得多。

That said, there's overhead in combining chunks into a single string or bytes object. 就是说,将块组合成单个字符串或字节对象存在开销。 If what you have are chunks, and the only reason you're combining them is for the hash performance, that probably will not save time. 如果您拥有的是块,而将它们组合在一起的唯一原因是为了提高哈希性能,那可能就不会节省时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM