Python FNV hash implementation based on cython, to give you an alternative high speed choice. - GoodManWEN/py-fnvhash-c
Project description
fnvhash-c
Python FNV hash implementation based on cython, to give you an alternative choice when you need a high speed hash in python, could reduce the latency of a single call to tens of nanoseconds.
Because of deprecation, we selectively did not implement FNV-0.
With code structure referenced to https://github.com/znerol/py-fnvhash
Install
pip install fnvhash-c
Feature
- Implementation in pure C, you can expect a 30x to 50x speedup.
- Hash latency reduce to under 100ns.
- A simple bloom filter built inside , which uses a slightly different parameter than the default value to keep result diverse. Ultra fast to use for distributed blacklist scenarios.
- CityHash included, since the original repo is no longer maintained, it is difficult to compile directly on windows.
Documentation
https://fnvhash-c.readthedocs.io
Example
# example.py
import fnvhash_c
print(fnvhash_c.fnv1_32(b'Hello world!'))
print(fnvhash_c.fnv1a_32(b'Hello world!'))
print(fnvhash_c.fnv1_64(b'Hello world!'))
print(fnvhash_c.fnv1a_64(b'Hello world!'))
center = fnvhash_c.BloomFilter(capability = 4096)
# To reduce runtime overhead, The default capacity is set to a constant compiled in the program.
# If you wish to change it, you need to compile the libs yourself to make sure the program work fine.
# Generally speaking, depending on the conversion time between Python and C,
# a shorter capability usually helps to make filter run faster.
import random
random_char_generator = lambda : f"{random.randint(1000000000,9999999999)}+salt".encode()
test_time = 1000000
hit = 0
miss = 0
for _ in range(test_time):
if center.hit(random_char_generator()):
hit += 1
else:
miss += 1
# Since the filter is total blank ,the hit rate should be very low
assert (hit * 100 / test_time) <= 0.01
print(f"Empty filter hit rate: {round(hit * 100 / test_time)}%")
# Now we put something into the filter list.
black_list = [random_char_generator() for _ in range(10000)]
for char in black_list:
center.update(char)
hit = 0
for _ in range(test_time):
if center.hit(random_char_generator()):
hit += 1
print(f"Filter with 10k elements hit rate: {round(hit * 100 / test_time,2)}%")
hit = 0
for char in black_list:
if center.hit(char):
hit += 1
print(f"Items in the blacklist hit rate: {round(hit * 100 / len(black_list),2)}%", ) # should be 100%
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
File details
Details for the file fnvhash_c-0.1.4-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: fnvhash_c-0.1.4-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 45.0 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb2e795443b520dbc48fe98a9b18cda8a01abee11e1bc725926de1920c1ba80b |
|
MD5 | 3e43e2d3085b508080d547f323323af3 |
|
BLAKE2b-256 | 73dd8ba1114a3a529dd4fd40d3e1cc2749210062721459900da01e02565576a8 |
File details
Details for the file fnvhash_c-0.1.4-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: fnvhash_c-0.1.4-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 45.4 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.8.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f9dc34c50f09ae4cdd240e92def1235f01cc2e23dcbd43a4575317899ba868fb |
|
MD5 | 313d1a504f608afdad91751d88f6b630 |
|
BLAKE2b-256 | 73c547c30e1654425e5330af9746f487a8ae2de05986c8bd33219a2f30f2d649 |
File details
Details for the file fnvhash_c-0.1.4-cp37-cp37m-win_amd64.whl
.
File metadata
- Download URL: fnvhash_c-0.1.4-cp37-cp37m-win_amd64.whl
- Upload date:
- Size: 44.9 kB
- Tags: CPython 3.7m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.7.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45f53a5ffbf3776a50fd42f01f9172fca111f3dea15aa435be9298f762a67550 |
|
MD5 | 54a31daad3018ca5863369d22dd0e943 |
|
BLAKE2b-256 | fda3fd3371d72cc2e24ab683eac235ce6401aca9f850d8e0e73b2d6e88807b08 |
File details
Details for the file fnvhash_c-0.1.4-cp36-cp36m-win_amd64.whl
.
File metadata
- Download URL: fnvhash_c-0.1.4-cp36-cp36m-win_amd64.whl
- Upload date:
- Size: 44.8 kB
- Tags: CPython 3.6m, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.6.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6dd8a4acbfc6012e39aab806caa81bf1b2004cfdf890ebf221edfaa3641e545f |
|
MD5 | ea57de15cce6cd87de62b98d8d335979 |
|
BLAKE2b-256 | be87474e12a66186a4707e943c176c7bfa0fc1e321d786f7b14815f10282c0e4 |