Skip to content

Instantly share code, notes, and snippets.

@afraca
Created June 29, 2012 08:19
Show Gist options
  • Save afraca/3016624 to your computer and use it in GitHub Desktop.
Save afraca/3016624 to your computer and use it in GitHub Desktop.
Hash algo timing in python
from timeit import default_timer
import hashlib
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
self.timer = default_timer
def __enter__(self):
self.start = self.timer()
return self
def __exit__(self, *args):
end = self.timer()
self.elapsed_secs = end - self.start
self.elapsed = self.elapsed_secs * 1000 # millisecs
if self.verbose:
print('elapsed time: %f ms' % self.elapsed)
myset = {}
for algo in hashlib.algorithms_available:
with Timer() as t:
hasher = hashlib.new(algo)
for i in range(100000):
hasher.update(b"some random string")
myset[algo] = t.elapsed
print(sorted(myset.items(), key=lambda x: x[1]))
@afraca
Copy link
Author

afraca commented Jun 29, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment