Skip to content

Instantly share code, notes, and snippets.

@yamionp
Created March 2, 2014 11:11
Show Gist options
  • Save yamionp/9305052 to your computer and use it in GitHub Desktop.
Save yamionp/9305052 to your computer and use it in GitHub Desktop.
# -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import unicode_literals
from __future__ import print_function
from collections import Counter
import hashlib
import mmh3
import timeit
import uuid
import numpy
def _t(func, start=0, cnt=100000):
return numpy.array(Counter([
func(bytes(i)) % 16
for i
in xrange(start, start + cnt)]).values())
def _time(cnt=100000):
print(timeit.timeit('int(hashlib.md5(x).hexdigest(), 16)',
number=cnt))
def _d(arr):
ma = numpy.max(arr)
mi = numpy.min(arr)
return ma, mi, numpy.var(arr)
if __name__ == '__main__':
cnt = 1000
print('int', '-' * 20)
print(_d(_t(lambda x: int(x))))
print(_d(_t(lambda x: int(x), start=10000000)))
print('md5', '-' * 20)
print(_d(_t(lambda x: int(hashlib.md5(x).hexdigest(), 16), )))
print(_d(_t(lambda x: int(hashlib.md5(x).hexdigest(), 16),
start=10000000)))
print(timeit.timeit(
'import hashlib;'
'[int(hashlib.md5(bytes(i)).hexdigest(), 16) for i in range(100)]',
number=cnt))
print('sha1', '-' * 20)
print(_d(_t(lambda x: int(hashlib.sha1(x).hexdigest(), 16))))
print(_d(_t(lambda x: int(hashlib.sha1(x).hexdigest(), 16),
start=10000000)))
print(timeit.timeit(
'import hashlib;'
'[int(hashlib.sha1(bytes(i)).hexdigest(), 16) for i in range(100)]',
number=cnt))
print('sha1 g', '-' * 20)
print(_d(_t(lambda x: int(hashlib.sha1(x).hexdigest()[-2:], 16))))
print(_d(_t(lambda x: int(hashlib.sha1(x).hexdigest()[-2:], 16),
start=10000000)))
print(timeit.timeit(
'import hashlib;'
'[int(hashlib.sha1(bytes(i)).hexdigest()[-2:], 16) for i in range(100)]',
number=cnt))
print('sha256', '-' * 20)
print(_d(_t(lambda x: int(hashlib.sha256(x).hexdigest(), 16))))
print(_d(_t(lambda x: int(hashlib.sha256(x).hexdigest(), 16),
start=10000000)))
print(timeit.timeit(
'import hashlib;'
'[int(hashlib.sha256(bytes(i)).hexdigest(), 16) for i in range(100)]',
number=cnt))
print('mmh3', '-' * 20)
print(_d(_t(mmh3.hash)))
print(_d(_t(mmh3.hash, start=10000000)))
print(timeit.timeit(
'import mmh3;'
'[mmh3.hash(bytes(i)) for i in range(100)]',
number=cnt))
print('uuid4', '-' * 20)
print(_d(_t(lambda x: uuid.uuid4().int)))
print(timeit.timeit(
'import uuid;'
'[uuid.uuid4().int for i in range(100)]',
number=cnt))
int --------------------
(6250, 6250, 0.0)
(6250, 6250, 0.0)
md5 --------------------
(6459, 6103, 6364.5)
(6319, 6126, 3141.0)
0.240433931351
sha1 --------------------
(6364, 6054, 7377.125)
(6414, 6085, 7957.0)
0.261728048325
sha1 g --------------------
(6364, 6054, 7377.125)
(6414, 6085, 7957.0)
0.240010023117
sha256 --------------------
(6367, 6169, 2956.0)
(6347, 6144, 4402.375)
0.292956113815
mmh3 --------------------
(6382, 6031, 8924.0)
(6372, 6135, 3748.875)
0.034991979599
uuid4 --------------------
(6441, 6122, 7487.5)
2.73583602905
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment