Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-99305:secrets.token_hex() speeded up 2x#99306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gpshead merged 5 commits intopython:mainfromNewUserHa:patch-1
Nov 11, 2022

Conversation

NewUserHa
Copy link
Contributor

@NewUserHaNewUserHa commentedNov 9, 2022
edited
Loading

@ghost
Copy link

ghost commentedNov 9, 2022
edited by ghost
Loading

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

Most changes to Pythonrequire a NEWS entry.

Please add it using theblurb_it web app or theblurb command-line tool.

@AlexWaygoodAlexWaygood added type-featureA feature request or enhancement performancePerformance or resource usage labelsNov 10, 2022
@eendebakpt
Copy link
Contributor

@NewUserHa Can you sign the CLA and write a news entry? The improvement looks good

@NewUserHa
Copy link
ContributorAuthor

Done.

@eendebakpt
Copy link
Contributor

The speed improvement depends on a bit on the number of bytes requested. A benchmark also taking thetoken_bytes into account:

nbytes=2048%timeit binascii.hexlify(token_bytes(nbytes)).decode('ascii') %timeit token_bytes(nbytes).hex() # 3.5 µs ± 93.9 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)# 3 µs ± 106 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)nbytes=32%timeit binascii.hexlify(token_bytes(nbytes)).decode('ascii') %timeit token_bytes(nbytes).hex() # 574 ns ± 62.9 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)# 426 ns ± 41.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)nbytes=1%timeit binascii.hexlify(token_bytes(nbytes)).decode('ascii') %timeit token_bytes(nbytes).hex() # 414 ns ± 16.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)# 281 ns ± 2.71 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

…zQc3.rstCo-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>
@NewUserHa
Copy link
ContributorAuthor

right, takingtoken_bytes into account is only 1.2~1.4 time faster.
on my device

nbytes=2048%timeitbinascii.hexlify(token_bytes(nbytes)).decode('ascii')%timeittoken_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 fasternbytes=32%timeitbinascii.hexlify(token_bytes(nbytes)).decode('ascii')%timeittoken_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 fasternbytes=1%timeitbinascii.hexlify(token_bytes(nbytes)).decode('ascii')%timeittoken_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 faster
t=os.urandom(2048)%timeitbinascii.hexlify(t).decode('ascii')%timeitt.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 fastert=os.urandom(32)%timeitbinascii.hexlify(t).decode('ascii')%timeitt.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 fastert=os.urandom(1)%timeitbinascii.hexlify(t).decode('ascii')%timeitt.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 faster

return_Py_strhex_with_sep(argbuf,arglen,sep,bytes_per_sep);

binascii_hexlify_impl(PyObject*module,Py_buffer*data,PyObject*sep,

binascii.hexlify() works the same withbytes.hex() +encode() but has different implements. merging may get anotherfree performance boost that can faster python.

@illia-v
Copy link
Contributor

import binascii can be removed from the module probably

@NewUserHa
Copy link
ContributorAuthor

right,import binascii removed.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@gpsheadgpsheadgpshead approved these changes

@eendebakpteendebakpteendebakpt approved these changes

@sweeneydesweeneydeAwaiting requested review from sweeneyde

Assignees
No one assigned
Labels
performancePerformance or resource usagetype-featureA feature request or enhancement
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@NewUserHa@bedevere-bot@eendebakpt@illia-v@gpshead@AlexWaygood

[8]ページ先頭

©2009-2025 Movatter.jp