Created
June 29, 2012 08:19
-
-
Save afraca/3016624 to your computer and use it in GitHub Desktop.
Hash algo timing in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Timer class:
https://gist.github.com/2942781