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

stdlibsecrets.token_hex() speeded up 2x #99305

Closed
Labels
3.12only security fixesperformancePerformance or resource usagestdlibPython modules in the Lib dirtype-featureA feature request or enhancement
@NewUserHa

Description

@NewUserHa

Feature or enhancement

CPython 3.11 is on average25% 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

deftoken_hex(nbytes=None):
"""Return a random text string, in hexadecimal.
The string has *nbytes* random bytes, each byte converted to two
hex digits. If *nbytes* is ``None`` or not supplied, a reasonable
default is used.
>>> token_hex(16) #doctest:+SKIP
'f9bf78b9a18ce6d46a0cd2b0b86df9da'
"""
returnbinascii.hexlify(token_bytes(nbytes)).decode('ascii')

importost=os.urandom(32)# Before:timeitbinascii.hexlify(t).decode('ascii')# 215 ns ± 6.7 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)# After:timeitt.hex()# 91 ns ± 1.6 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesperformancePerformance or resource usagestdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp