ulid
ulid copied to clipboard
Use rand.randbytes() instead of os.urandom()
[mmarkk@asus home]$ python -m timeit -s 'import random' 'random.randbytes(8)'
5000000 loops, best of 5: 93.9 nsec per loop
[mmarkk@asus home]$ python -m timeit -s 'import os' 'os.urandom(8)'
1000000 loops, best of 5: 248 nsec per loop
randbytes is not cryptographically secure see https://docs.python.org/3/library/random.html#random.randbytes vs https://docs.python.org/3/library/os.html#os.urandom
@Zaczero but ulid is not about security/cryptography, right ? But speed is important.
Depends on the implementation, this specific one relies on cryptographically secure randomness: https://github.com/ahawker/ulid#components. ULID spec also recommends doing so: https://github.com/ulid/spec#components.
If you don't care about unpredictability, maybe consider using the thread lock feature - https://github.com/ahawker/ulid#thread-lock - it removes the randomness for quick sequential id generations.