gh-99305: secrets.token_hex() speeded up 2x#99306
Merged
gpshead merged 5 commits intopython:mainfrom Nov 11, 2022
Merged
Conversation
|
Most changes to Python require a NEWS entry. Please add it using the blurb_it web app or the blurb command-line tool. |
Contributor
|
@NewUserHa Can you sign the CLA and write a news entry? The improvement looks good |
Contributor
Author
|
Done. |
Contributor
|
The speed improvement depends on a bit on the number of bytes requested. A benchmark also taking the |
eendebakpt
reviewed
Nov 10, 2022
Misc/NEWS.d/next/Library/2022-11-10-11-51-39.gh-issue-99305.6LzQc3.rst
Outdated
Show resolved
Hide resolved
eendebakpt
approved these changes
Nov 10, 2022
…zQc3.rst Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
Contributor
Author
|
right, taking nbytes=2048
%timeit binascii.hexlify(token_bytes(nbytes)).decode('ascii')
%timeit token_bytes(nbytes).hex()
# 4.86 µs ± 449 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
# 3.93 µs ± 87.3 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
# 1.2366412213740458015267175572519 faster
nbytes=32
%timeit binascii.hexlify(token_bytes(nbytes)).decode('ascii')
%timeit token_bytes(nbytes).hex()
# 743 ns ± 66.8 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# 528 ns ± 28 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# 1.407196969696969696969696969697 faster
nbytes=1
%timeit binascii.hexlify(token_bytes(nbytes)).decode('ascii')
%timeit token_bytes(nbytes).hex()
# 652 ns ± 80.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# 442 ns ± 53.6 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# 1.2366412213740458015267175572519 fastert = os.urandom(2048)
%timeit binascii.hexlify(t).decode('ascii')
%timeit t.hex()
# 3.69 µs ± 579 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
# 2.78 µs ± 349 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
# 1.3273381294964028776978417266187 faster
t = os.urandom(32)
%timeit binascii.hexlify(t).decode('ascii')
%timeit t.hex()
# 323 ns ± 112 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# 116 ns ± 3.33 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
# 2.7844827586206896551724137931034 faster
t = os.urandom(1)
%timeit binascii.hexlify(t).decode('ascii')
%timeit t.hex()
# 221 ns ± 13.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)
# 77.4 ns ± 1.97 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)
# 2.8552971576227390180878552971576 fasterLine 2482 in cb04a09 cpython/Modules/clinic/binascii.c.h Line 486 in f07adf8 binascii.hexlify() works the same with bytes.hex() + encode() but has different implements. merging may get another free performance boost that can faster python.
|
Contributor
|
|
Contributor
Author
|
right, |
gpshead
approved these changes
Nov 11, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
secrets.token_hex()speeded up 2x #99305