* One more improvement is that the previous popcount-based code fo。萬萬沒想到,除了香農計劃,Python3.11竟還有這么多性能提升!( 三 )。" />

萬萬沒想到,除了香農計劃,Python3.11竟還有這么多性能提升!( 三 )

n ^ k ^ (n - k). python/blob/03dccc557adf39db0150410e7c448ff3164e7022/Modules/mathmodule.c#L3583" rel="external nofollow noreferrer">*
One more improvement is that the previous popcount-based code for computing the largest power of two dividing math.comb(n, k) (for small n) got replaced with a more direct method based on counting trailing zeros of the factorials involved. (python/cpython/pull/30313#issue-1091542983" rel="external nofollow noreferrer">*).
Python 3.10:
$ python -m pyperf timeit -s \'import math' -- 'math.comb(100, 55)'.....................Mean +- std dev: 3.72 us +- 0.07 us# ---$ python -m pyperf timeit -s \'import math' -- 'math.comb(10000, 5500)'.....................Mean +- std dev: 11.9 ms +- 0.1 msPython 3.11:
$ python -m pyperf timeit -s \'import math' -- 'math.comb(100, 55)'.....................Mean +- std dev: 476 ns +- 20 ns# ---$ python -m pyperf timeit -s \'import math' -- 'math.comb(10000, 5500)'.....................Mean +- std dev: 2.28 ms +- 0.10 ms對于 statistics 庫:優化了 mean(data)、variance(data, xbar=None) 與 stdev(data, xbar=None)3.11 優化了statistics模塊中的 mean、variancestdev 函數 。如果入參是一個迭代器 , 則會直接用于計算,而不是先將其轉換為列表 。這種python/blob/208abcd8f1726646f8d86306616b0db802d8064c/Lib/statistics.py#L205" rel="external nofollow noreferrer">計算方法 的速度比之前的快了一倍 。python.org/3/whatsnew/changelog.html" rel="external nofollow noreferrer">*
Python 3.10:
# Mean$ python -m pyperf timeit -s \'import statistics' -- 'statistics.mean(range(1_000))'.....................Mean +- std dev: 255 us +- 11 us# Variance$ python -m pyperf timeit -s \'import statistics' -- 'statistics.variance((x * 0.1 for x in range(0, 10)))'.....................Mean +- std dev: 77.0 us +- 2.9 us# Sample standard deviation (stdev)$ python -m pyperf timeit -s \'import statistics' -- 'statistics.stdev((x * 0.1 for x in range(0, 10)))'.....................Mean +- std dev: 78.0 us +- 2.2 usPython 3.11:
# Mean$ python -m pyperf timeit -s \'import statistics' -- 'statistics.mean(range(1_000))'.....................Mean +- std dev: 193 us +- 7 us# Variance$ python -m pyperf timeit -s \'import statistics' -- 'statistics.variance((x * 0.1 for x in range(0, 10)))'.....................Mean +- std dev: 56.1 us +- 2.3 us# Sample standard deviation (stdev)$ python -m pyperf timeit -s \'import statistics' -- 'statistics.stdev((x * 0.1 for x in range(0, 10)))'.....................Mean +- std dev: 59.4 us +- 2.6 us純 ASCII 字符串的 unicodedata.normalize(),提升到常數時間對于 unicodedata.normalize() 方法 , 如果提供的入參是純 ASCII 字符串 , 則通過 unicode 快速檢查算法 迅速返回結果 。這項檢查使用的是PyUnicode_IS_ASCII 實現 。
Python 3.10:
$ python -m pyperf timeit -s \'import unicodedata' -- 'unicodedata.normalize("NFC", "python")'.....................Mean +- std dev: 83.3 ns +- 4.3 nsPython 3.11:
$ python -m pyperf timeit -s \'import unicodedata' -- 'unicodedata.normalize("NFC", "python")'.....................Mean +- std dev: 34.2 ns +- 1.2 ns最后的話:

  • 我寫這篇文章是為了加深自己對 Python 3.11 最新成果的認識 。如果內容有錯,請通過email 或者 Twitter告訴我 。(譯注:本翻譯是出于促進自己學習及加強理解的目的,若有錯漏,歡迎指正?。?/li>
  • 附 HackerNews 上的評論
  • 在下一篇文章中 , 我將分析 faster CPython 項目帶來的優化點 。敬請期待!
【萬萬沒想到,除了香農計劃,Python3.11竟還有這么多性能提升!】

推薦閱讀