ahawker / ulid

Universally Unique Lexicographically Sortable Identifier (ULID) in Python 3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use rand.randbytes() instead of os.urandom()

socketpair opened this issue · comments

[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

@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.