File: benchmark_hashes.py

package info (click to toggle)
python-uhashring 1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 156 kB
  • sloc: python: 668; makefile: 21
file content (68 lines) | stat: -rw-r--r-- 1,643 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
"""This is not part of the test suite.
"""
from hashlib import md5, sha1, sha256
from time import time

num = 1000000
print("running {} key generation comparison".format(num))

# md5
pt = time()
for i in range(num):
    key = b"myval-%d" % i
    int(md5(b"%s" % key).hexdigest(), 16)
print("md5 took {} s".format(time() - pt))

# sha1
pt = time()
for i in range(num):
    key = b"myval-%d" % i
    int(sha1(b"%s" % key).hexdigest(), 16)
print("sha1 took {} s".format(time() - pt))

# sha256
pt = time()
for i in range(num):
    key = b"myval-%d" % i
    int(sha256(b"%s" % key).hexdigest(), 16)
print("sha256 took {} s".format(time() - pt))

# CityHash
try:
    from cityhash import CityHash32, CityHash64, CityHash128
except ImportError:
    print("CityHash not installed, pip install cityhash")
else:
    # cityhash32
    pt = time()
    for i in range(num):
        key = b"myval-%d" % i
        CityHash32(b"%s" % key)
    print("CityHash32 took {} s".format(time() - pt))

    # cityhash64
    pt = time()
    for i in range(num):
        key = b"myval-%d" % i
        CityHash64(b"%s" % key)
    print("CityHash64 took {} s".format(time() - pt))

    # cityhash128
    pt = time()
    for i in range(num):
        key = b"myval-%d" % i
        CityHash128(b"%s" % key)
    print("CityHash128 took {} s".format(time() - pt))

# murmurhash v3
try:
    import mmh3
except ImportError:
    print("MurmurHash v3 not installed, pip install murmurhash3")
else:
    pt = time()
    for i in range(num):
        key = b"myval-%d" % i
        mmh3.hash(b"%s" % key)
    print("MurmurHash v3 took {} s".format(time() - pt))