bytes.fromhex() is 1.3 times slower than binascii.a2b_hex()
未關閉
還沒有人認領這個 Issue。
interpreter-core
pending
performance
type-bug
- 主要語言
- Python
- 星號
- 77.2k
- 分支
- 36k
- PR 合併指標
- PR 指標待擷取
描述
Bug report
Bug description:
I was looking at modernising some old code that uses binascii.a2b_hex, and I was surprised to find that bytes.fromhex() is a lot slower:
>>> timeit.timeit('bytes.fromhex("abcd1234abcd1234")', number=10000000)
1.0542256180197
>>> timeit.timeit('binascii.a2b_hex("abcd1234abcd1234")', 'import binascii', number=10000000)
0.4072963949292898
I thought maybe it was a difference in the lookup of the bytes classmethod vs the module lookup. It appears that it is, to some extent, but not completely:
>>> timeit.timeit('fromhex("abcd1234abcd1234")', 'fromhex=bytes.fromhex', number=10000000)
0.6133267850382254
>>> timeit.timeit('a2b_hex("abcd1234abcd1234")', 'from binascii import a2b_hex', number=10000000)
0.3788172130007297
Of course, all these times are extremely small, so it probably does not matter in most situations, but still, I found it surprising.
CPython versions tested on:
3.13
Operating systems tested on:
Linux
貢獻指南
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
研究方向
先在 CPython 3.13/Linux 上重現 issue 中展示的 bytes.fromhex() 和 binascii.a2b_hex() 的 timeit 比較,接著追蹤這兩個已命名的進入點,以了解差異的來源。有用的成果應確定是否能讓 bytes.fromhex() 變得更快,並使用等效的基準測試和相關測試驗證任何變更。
由索引模型根據 Issue 內容生成。
評估
- 技術堆疊
- python
- 領域
- performance
- Issue 類型
- 缺陷
- 難度
- 4/5
- 預估耗時
- 3-5 天
- 活躍度
- 冷清
- 描述清晰度
- 需要釐清
- 新手友好度
- 45/100