cpython
cpython copied to clipboard
stdlib `secrets.token_hex()` speeded up 2x
Feature or enhancement
CPython 3.11 is on average 25% faster than CPython 3.10
Therefore hope the python 3.12 stdlibs can get optimized as well, since maybe web could use secrets tokens a lot
Pitch
https://github.com/python/cpython/blob/7c9c993945a63dc774554a8cc5ee1ef62a2454c7/Lib/secrets.py#L48-L59
import os
t = os.urandom(32)
# Before:
timeit binascii.hexlify(t).decode('ascii') # 215 ns ± 6.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# After:
timeit t.hex() # 91 ns ± 1.6 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
- PR: gh-99306