python / python/cpython

bytes.fromhex() is 1.3 times slower than binascii.a2b_hex()

オープン
#131,171 コメント 23 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

interpreter-core pending performance type-bug
主要言語
Python
スター
77.2k
フォーク
35.9k
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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、issue に示されている bytes.fromhex() と binascii.a2b_hex() の timeit 比較を CPython 3.13/Linux 上で再現し、次に名前が示された 2 つのエントリポイントをたどって差異の原因を理解してください。有用な成果は、bytes.fromhex() を高速化できるかどうかを明らかにし、同等のベンチマークと関連するテストで変更を検証することです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
python
領域
performance
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
静か
明瞭さ
説明が足りない
初心者へのやさしさ
45/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。