Skip to content

Instantly share code, notes, and snippets.

@hinnerk
Created January 7, 2014 17:00
Show Gist options
  • Save hinnerk/8302489 to your computer and use it in GitHub Desktop.
Save hinnerk/8302489 to your computer and use it in GitHub Desktop.
My self written RollingHash was slower than SHA-1… (written ~2007)
import hashlib
from backup import hashes
import timeit
import cProfile
def rechne_sha(filename):
block = open(filename, "rb").read()
for i in range (512,len(block)):
[hashlib.sha1(block[i:i+512]).hexdigest()]
def rechne_wh(filename):
block = open(filename, "rb").read()
wh = hashes.RollingHash(block[0:512])
# speed: take method lookup out of loop
wu = wh.update
for i in range (512,len(block)):
wu(block[i])
ts = timeit.Timer('rechne_sha("Beispiel.pdf")', 'from __main__ import rechne_sha')
tw = timeit.Timer('rechne_wh("Beispiel.pdf")', 'from __main__ import rechne_wh')
anzahl = 10
def runner(anzahl):
for i in range(0, anzahl):
rechne_wh("Beispiel.pdf")
print "Sha1:\t%s\nWH:\t%s" %(ts.timeit(anzahl), tw.timeit(anzahl))
cProfile.run('runner(10)')
# Sha1: 5.26615405083
# WH: 2.6121430397
# 3384084 function calls in 3.717 CPU seconds
#
# Ordered by: standard name
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1 0.000 0.000 3.717 3.717 <string>:1(<module>)
# 10 0.508 0.051 3.716 0.372 cryptbench.py:13(rechne_wh)
# 1 0.000 0.000 3.717 3.717 cryptbench.py:26(runner)
# 10 0.000 0.000 0.002 0.000 hashes.py:27(__init__)
# 10 0.000 0.000 0.002 0.000 hashes.py:42(_digest)
# 845990 2.639 0.000 3.190 0.000 hashes.py:50(update)
# 20 0.000 0.000 0.000 0.000 {len}
# 10 0.001 0.000 0.001 0.000 {map}
# 845990 0.113 0.000 0.113 0.000 {method 'append' of 'list' objects}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
# 845990 0.333 0.000 0.333 0.000 {method 'pop' of 'list' objects}
# 10 0.003 0.000 0.003 0.000 {method 'read' of 'file' objects}
# 10 0.000 0.000 0.000 0.000 {open}
# 845990 0.104 0.000 0.104 0.000 {ord}
# 21 0.014 0.001 0.014 0.001 {range}
# 20 0.001 0.000 0.001 0.000 {sum}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment