wyhash
The FASTEST QUALITY hash function, random number generators (PRNG) and hash map.
Top Related Projects
Hash function quality and speed tests
Extremely fast non-cryptographic hash algorithm
Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
Quick Overview
WYHash is a fast, portable hash function and random number generator library. It aims to provide high-quality hashing and random number generation with minimal code and excellent performance across various platforms and architectures.
Pros
- Extremely fast performance, often outperforming other popular hash functions
- Simple implementation with minimal code, making it easy to integrate and maintain
- Portable across different platforms and architectures
- Passes statistical tests for randomness and distribution
Cons
- Relatively new compared to some well-established hash functions
- Limited cryptographic security guarantees (not designed for cryptographic use)
- May require more extensive real-world testing and validation
Code Examples
- Basic hashing example:
#include "wyhash.h"
uint64_t hash = wyhash("Hello, World!", 13, 0);
This code hashes the string "Hello, World!" using WYHash.
- Random number generation:
#include "wyhash.h"
uint64_t seed = 42;
uint64_t random_number = wyrand(&seed);
This example generates a random 64-bit number using WYHash's random number generator.
- Floating-point random number:
#include "wyhash.h"
uint64_t seed = 42;
double random_float = wy2u01(wyrand(&seed));
This code generates a random floating-point number between 0 and 1.
Getting Started
To use WYHash in your project:
- Download the
wyhash.h
file from the repository. - Include the header in your C/C++ project:
#include "wyhash.h"
-
Use the functions as shown in the examples above. No compilation or linking of additional libraries is required.
-
For random number generation, initialize a seed variable:
uint64_t seed = time(NULL); // Or any other seed value
- Use the seed with
wyrand()
or other random functions as needed.
Competitor Comparisons
Hash function quality and speed tests
Pros of SMHasher
- Comprehensive test suite for hash functions
- Supports a wide range of hash algorithms
- Provides detailed performance and quality metrics
Cons of SMHasher
- More complex to use and understand
- Larger codebase with multiple dependencies
- Slower execution due to extensive testing
Code Comparison
SMHasher (test execution):
#include "SMHasher.h"
int main(int argc, char ** argv) {
TestHashList(argc, argv);
return 0;
}
wyhash (hash function implementation):
static inline uint64_t wyhash(const void *key, uint64_t len, uint64_t seed) {
const uint8_t *p = (const uint8_t *)key;
uint64_t a, b;
if (len <= 16) {
// ... (hash calculation for short keys)
}
// ... (hash calculation for longer keys)
}
SMHasher focuses on testing and benchmarking various hash functions, while wyhash provides a simple, fast hash function implementation. SMHasher is more suitable for researchers and developers evaluating hash function quality, while wyhash is designed for practical use in applications requiring a quick and efficient hash function.
Extremely fast non-cryptographic hash algorithm
Pros of xxHash
- Widely adopted and battle-tested in various applications
- Excellent performance on x86 and ARM architectures
- Extensive documentation and API support
Cons of xxHash
- Slightly larger code footprint compared to wyhash
- May have slower performance on some non-x86/ARM platforms
Code Comparison
xxHash:
XXH64_hash_t XXH64(const void* input, size_t length, XXH64_hash_t seed)
{
// Complex implementation with multiple branches and loops
}
wyhash:
uint64_t wyhash(const void* key, uint64_t len, uint64_t seed)
{
// Simpler implementation with fewer branches
}
Key Differences
- xxHash offers multiple variants (32-bit, 64-bit, 128-bit) while wyhash focuses on 64-bit
- wyhash claims to be faster and simpler, especially on non-x86 platforms
- xxHash has a more extensive ecosystem and third-party language bindings
Use Cases
- xxHash: Ideal for applications requiring a well-established, widely-supported hash function
- wyhash: Suitable for projects prioritizing simplicity and performance across various platforms
Community and Support
- xxHash: Larger community, more frequent updates, and extensive documentation
- wyhash: Smaller community but active development and responsive maintainer
Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
Pros of robin-hood-hashing
- Implements Robin Hood hashing, which can provide better performance in certain scenarios
- More comprehensive library with additional features and data structures
- Well-documented with extensive benchmarks and examples
Cons of robin-hood-hashing
- Larger codebase and more complex implementation
- May have higher memory usage due to additional metadata
- Potentially slower for simple use cases or small datasets
Code Comparison
wyhash:
uint64_t wyhash(const void* key, uint64_t len, uint64_t seed) {
const uint8_t *p = (const uint8_t*)key;
uint64_t a, b;
if (len <= 16) {
// ... (simplified for brevity)
}
}
robin-hood-hashing:
template <typename Key, typename T, typename Hash = hash<Key>,
typename KeyEqual = equal_to<Key>>
class unordered_map {
public:
using key_type = Key;
using mapped_type = T;
// ... (additional type definitions and methods)
};
The wyhash repository focuses on a single, fast hashing function, while robin-hood-hashing provides a more comprehensive hash table implementation with additional features. wyhash is simpler and may be faster for basic hashing needs, while robin-hood-hashing offers more advanced functionality at the cost of increased complexity.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
wyhash has evolved into rapidhash !
With improved speed, quality and compatibility.
No hash function is perfect, but some are useful.
wyhash and wyrand are the ideal 64-bit hash function and PRNG respectively:
solid: wyhash passed SMHasher, wyrand passed BigCrush, practrand.
portable: 64-bit/32-bit system, big/little endian.
fastest: Efficient on 64-bit machines, especially for short keys.
simplest: In the sense of code size.
salted: We use dynamic secret to avoid intended attack.
wyhash is the default hashing algorithm of the great Zig, V, Nim and Go (since 1.17) language. One milestone is that wyhash has deployed by Microsoft on [Windows Terminal] (https://github.com/microsoft/terminal/pull/13686).
Simple Example:
#include "wyhash.h"
uint64_t _wyp[4];
make_secret(time(NULL),_wyp);
string s="fcdskhfjs";
uint64_t h=wyhash(s.c_str(),s.size(),0,_wyp);
Limitations:
It is known now that wyhash/wyrand have their limitations:
Both of them are not 64 bit collision resistant, but is about 62 bits (flyingmutant/Cyan4973/vigna)
When test on longer dataset (32TB, 23 days), wyrand will fail practrand (vigna)
And there may be more flaws detected in the future.
User should make their own decision based the advantage and the flaws of wyhash/wyrand as no one is perfect.
C# https://github.com/cocowalla/wyhash-dotnet
C++ https://github.com/tommyettinger/waterhash
C++ https://github.com/alainesp/wy
GO https://github.com/dgryski/go-wyhash
GO https://github.com/orisano/wyhash
GO https://github.com/littleli/go-wyhash16
GO https://github.com/zeebo/wyhash
GO https://github.com/lonewolf3739/wyhash-go
GO https://github.com/zhangyunhao116/wyhash (final version 1 && 3)
Java https://github.com/OpenHFT/Zero-Allocation-Hashing
Java https://github.com/dynatrace-oss/hash4j (final version 3 and 4)
Kotlin Multiplatform https://github.com/appmattus/crypto/tree/main/cryptohash
Nim https://github.com/nim-lang/Nim/blob/devel/lib/pure/hashes.nim
Nim https://github.com/jackhftang/wyhash.nim
Nim https://github.com/littleli/nim-wyhash16
Rust https://github.com/eldruin/wyhash-rs
Swift https://github.com/lemire/SwiftWyhash
Swift https://github.com/lemire/SwiftWyhashBenchmark
Swift https://github.com/jeudesprits/PSWyhash
V https://github.com/vlang/v/tree/master/vlib/hash/wyhash (v4)
Zig https://github.com/ManDeJan/zig-wyhash
absl hashmap https://github.com/abseil/abseil-cpp/blob/master/absl/hash/internal/low_level_hash.h
I thank these names:
Reini Urban
Dietrich Epp
Joshua Haberman
Tommy Ettinger
Daniel Lemire
Otmar Ertl
cocowalla
leo-yuriev
Diego Barrios Romero
paulie-g
dumblob
Yann Collet
ivte-ms
hyb
James Z.M. Gao
easyaspi314 (Devin)
TheOneric
flyingmutant
vigna
tansy
Top Related Projects
Hash function quality and speed tests
Extremely fast non-cryptographic hash algorithm
Fast & memory efficient hashtable based on robin hood hashing for C++11/14/17/20
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot