Python3.7で高速化・性能向上した要素(ドキュメントの確認)

Python3.7になって、ずいぶんと速度が高速化されたらしい! そう聞いて、何がどう高速化したのか、まずは公式ドキュメントで高速化の対象を調べてみました。

 

しかし、現時点ではほぼ英語情報で、文章も長く、知らない機能もてんこ盛りで、自分の技術力・英語力レベルでは、なんだか頭にすんなり入って来ません。。。

 

バクっと何が改善されたのか、もう少し簡単につかめないか?

そこで、日本語の短文で見出しをつけて、短文で理解できた形で意訳して、頭に一次記憶としてなんとか残せそうな形式にした、自分用のメモを記録することにしました。

 

 

◆Python3.7の新機能:

 

▼参照したサイト

本家  Python3.7リリースに関するサイト

www.python.org


日本語の公式サイト Python3.7の新機能

https://docs.python.org/ja/3/whatsnew/3.7.html

 

 

 

◆Python3.7の新機能(最適化 Optimization部分):

 

高速化については、「最適化(Optimizations)」の項にまとめられているようです。

日本語の公式サイト中の「最適化」の項 (日本語サイトだがほぼ英語)

https://docs.python.org/ja/3/whatsnew/3.7.html#optimizations

 

まずは、この部分について、日本語メモをつくることにしました。

 

 

▼日本語短文の見出し(理解のためにメモ)

  

1.[性能向上] Cで実装された各種関数の呼び出し処理


2.[時間短縮] Pythonの起動時間(Linuxで10%短縮、Macで最大30%短縮)


3.[速度向上] メソッド呼び出し(最大20%高速化)

4.[性能向上] asyncioモジュール


5.[時間短縮]  キーボードから入力する処理

6.[速度向上] ソート処理(最大40%~75%高速化)

7.[速度向上] dict.copy()処理(最大5.5倍の高速化)

8.[速度向上] hasattr() と getattr() 処理(約4倍の高速化)

9.[速度向上] 特定のUnicode文字の検索処理が遅い問題の改善(通常より最大25倍遅延から3倍遅延への改善)

10.[速度向上] named tupleの作成(4倍から6倍の高速化)

11.[速度向上] date.fromordinal() と date.fromtimestamp()の処理(最大30%の高速化)

12.[速度向上] os.fwalk() functionの処理(最大2倍の高速化)

13.[速度向上] shutil.rmtree() functionの処理(20%から40%の高速化)

14.[速度向上] 大文字と小文字を区別しないマッチングや正規表現の検索処理(最大20倍の高速化)

15.[速度向上] re.compile()でflagをintオブジェクトに変換する処理(Pytyon3.6より約10%の高速化)

16.[速度向上] 高負荷環境における、各種selectorsクラスのmodify() methodsの処理(約10%の高速化)

17.[性能向上] より一貫したコンパイラ最適化(AST optimizerによる整数畳み込み)

18.[性能向上] クラス生成や呼び出しの高速化と起動時間の短縮(クラス生成や呼び出しが1.5倍高速化、Python起動が最大10%短縮)

19.[速度向上] datetime.date と datetime.datetimeのインスタンス生成速度の目覚ましい高速化

20.[速度向上] array.arrayインスタンスの比較処理の高速化(10倍から70倍の高速化)

21.[速度向上] math.erf() と math.erfc() functionsの高速化(高速なCライブラリにより実装)

 

 

 

▼最適化(Optimization)の日本語メモ
 (理解のための短文メモ)

 

 
1.[性能向上] Cで実装された各種関数の呼び出し処理


Cで実装された標準ライブラリのメソッドを呼び出すときのオーバーヘッドが劇的に削減されました。これは、新しい呼び出し規約であるMETH_FASTCALL規約を使用するように、より多くのコードを移植することによって実現しました。

 

The overhead of calling many methods of various standard library classes implemented in C has been significantly reduced by porting more code to use the METH_FASTCALL convention
(Contributed by Victor Stinner in bpo-29300, bpo-29507, bpo-29452, and bpo-29286.)

 

 
※METH_FASTCALL規約は、Python3.6から導入された新しい呼び出し規約で、Python3.7で適用範囲を拡大することによってさらに性能を向上させたようです。

dsas.blog.klab.org

従来は、PythonからCの関数を呼び出すときには、順序引数をタプルでキーワード引数をdictで渡していましたが、Python3.6より、タプルの代わりにただの配列の先頭ポインタと順序引数の数を渡す、新しい呼び出す規約が登場し呼び出し側がスタックに積んだ引数をタプルに詰めなくても、そのまま渡せるようになったようです。

 

 

2.[時間短縮] Pythonの起動時間(Linuxで10%短縮、Macで最大30%短縮)

 

さまざまな最適化によりPythonの起動時間が短縮されました。
Linuxでは10%短縮して、MacOSでは最大30%まで短縮されました。

 

Various optimizations have reduced Python startup time by 10% on Linux and up to 30% on macOS.
(Contributed by Victor Stinner, INADA Naoki in bpo-29585, and Ivan Levkivskyi in bpo-31333.)

 

 

3.[速度向上] メソッド呼び出し(最大20%高速化)

 

メソッド呼び出しが最大20%高速になりました。
これは、バインドされたメソッドインスタンスの作成を避けるよう、バイトコードを変更したことで実現しています。

 

Method calls are now up to 20% faster due to the bytecode changes which avoid creating bound method instances.
(Contributed by Yury Selivanov and INADA Naoki in bpo-26110.)

 


4.[性能向上] asyncioモジュール

 

asyncioモジュールのよく利用される関数に対して、以下のとおり、目覚ましい最適化が行われました。:

 

・asyncio.get_event_loop()関数は、C言語で再実装されることにより、最大15倍の高速化が実現されました。
・asyncio.Futureのコールバック管理が最適化されました。
・asyncio.gather() が最大15%高速化されました。
・asyncio.sleep() は、delay argumentがzeroかnegativeの時に、最大2倍高速化されました。

 

The asyncio module received a number of notable optimizations for commonly used functions:

 

・The asyncio.get_event_loop() function has been reimplemented in C to make it up to 15 times faster.
 (Contributed by Yury Selivanov in bpo-32296.)
・asyncio.Future callback management has been optimized.
  (Contributed by Yury Selivanov in bpo-32348.)
・asyncio.gather() is now up to 15% faster.
  (Contributed by Yury Selivanov in bpo-32355.)
・asyncio.sleep() is now up to 2 times faster when the delay argument is zero or negative.
  (Contributed by Andrew Svetlov in bpo-32351.)
・The performance overhead of asyncio debug mode has been reduced.
  (Contributed by Antoine Pitrou in bpo-31970.)

 

 

5.[時間短縮]  キーボードから入力する処理

 

PEP 560 の成果により、キーボードからタイプした情報のインポート時間は削減されました。これは7番目の要素 ( 7.[速度向上] dict.copy()処理の高速化(最大5.5倍高速化)のことでしょうか? )によって、実現しています。よって、多くのキーボードをタイピングする処理(言語に応じた辞書変換も含むのでしょうか?)は、より高速化しました。

 

As a result of PEP 560 work, the import time of typing has been reduced by a factor of 7, and many typing operations are now faster. (Contributed by Ivan Levkivskyi in bpo-32226.)

 

 

6.[速度向上] ソート処理(最大40%~75%高速化)

 

sorted() と list.sort()は、通常のケースにおいて最大40%から70%の高速化が実現するように、最適化されました。

 

sorted() and list.sort() have been optimized for common cases to be up to 40-75% faster. (Contributed by Elliot Gorokhovsky in bpo-28685.)

 

 

7.[速度向上] dict.copy()処理(最大5.5倍の高速化)

 

dict.copy()は最大5.5倍の高速化が実現しました。

 

dict.copy() is now up to 5.5 times faster. (Contributed by Yury Selivanov in bpo-31179.)

 

 

8.[速度向上] hasattr() と getattr() 処理(約4倍の高速化)

 

hasattr() と getattr()は、特定の条件下で、約4倍の高速化が実現しました。
その条件とは、
 1.nameが検知されないこと  かつ
 2.objが、以下のいずれかによってoverrideされないこと
   ・object.__getattr__()
   ・object.__getattribute__()

 

hasattr() and getattr() are now about 4 times faster when name is not found and obj does not override object.__getattr__() or object.__getattribute__(). (Contributed by INADA Naoki in bpo-32544.)

 

 

9.[速度向上] 特定のUnicode文字の検索処理が遅い問題の改善(通常より最大25倍遅延から3倍遅延への改善)

 

特定のUnicode文字(例えば、ウクライナ語で使われる大文字のЄなど)が文字列に含まれる場合の検索処理は、従来は他の文字の検索と比較して最大25倍遅かったのですが、最悪の場合でも3倍遅いレベルに改善しました。

 

Searching for certain Unicode characters (like Ukrainian capital "Є") in a string was up to 25 times slower than searching for other characters. It is now only 3 times slower in the worst case. (Contributed by Serhiy Storchaka in bpo-24821.)

 

 

10.[速度向上] named tupleの作成(4倍から6倍の高速化)

 

collections.namedtuple() factoryの再実装により、named tupleの生成処理は4倍から6倍の高速化が実現しました。

 

The collections.namedtuple() factory has been reimplemented to make the creation of named tuples 4 to 6 times faster. (Contributed by Jelle Zijlstra with further improvements by INADA Naoki, Serhiy Storchaka, and Raymond Hettinger in bpo-28638.)

 


11.[速度向上] date.fromordinal() と date.fromtimestamp()の処理(最大30%の高速化)

 

date.fromordinal() と date.fromtimestamp()は、一般的なケースにおいて、最大30%高速化しました。

 

date.fromordinal() and date.fromtimestamp() are now up to 30% faster in the common case. (Contributed by Paul Ganssle in bpo-32403.)

 


12.[速度向上] os.fwalk() functionの処理(最大2倍の高速化)

 

os.fwalk() ファンクションの処理速度は、最大2倍、高速化しました。
これは、os.scandir()ファンクションの使用により実現しました。

 

The os.fwalk() function is now up to 2 times faster thanks to the use of os.scandir(). (Contributed by Serhiy Storchaka in bpo-25996.)

 

 

13.[速度向上] shutil.rmtree() functionの処理(20%から40%の高速化)

 

shutil.rmtree() ファンクションの処理速度は、20%から40%高速化しました。
これは、os.scandir()ファンクションの使用により実現しました。

 

The speed of the shutil.rmtree() function has been improved by 20--40% thanks to the use of the os.scandir() function. (Contributed by Serhiy Storchaka in bpo-28564.)

 


14.[速度向上] 大文字と小文字を区別しないマッチングや正規表現の検索処理(最大20倍の高速化)

 

大文字と小文字を区別しないマッチング処理や正規表現の検索処理の最適化を行いました。いくつかのパターンの検索処理では、20倍の高速化を実現しています。

 

Optimized case-insensitive matching and searching of regular expressions. Searching some patterns can now be up to 20 times faster. (Contributed by Serhiy Storchaka in bpo-30285.)

 


15.[速度向上] re.compile()でflagをintオブジェクトに変換する処理(Pytyon3.6より約10%の高速化)

 

re.compile()は、flagパラメータがRegexFragである場合に、intオブジェクトに変換します。それによって、Python3.5と同等の処理速度を実現し、変換のパターンによるもののPython3.6より約10%の高速化を実現しています。

 

re.compile() now converts flags parameter to int object if it is RegexFlag. It is now as fast as Python 3.5, and faster than Python 3.6 by about 10% depending on the pattern. (Contributed by INADA Naoki in bpo-31671.)

 


16.[速度向上] 高負荷環境における、各種selectorsクラスのmodify() methodsの処理(約10%の高速化)

 

以下のクラスのmodify()メソッドは、高負荷環境において約10%の高速化が実現しました。
・selectors.EpollSelector
・selectors.PollSelector
・selectors.DevpollSelector

 

The modify() methods of classes selectors.EpollSelector, selectors.PollSelector and selectors.DevpollSelector may be around 10% faster under heavy loads. (Contributed by Giampaolo Rodola' in bpo-30014)

 

 

17.[性能向上] より一貫したコンパイラ最適化(AST optimizerによる整数畳み込み)

 

コンパイラ最適化手法である)定数畳み込み機能の実装は、peehole optimizer から、新しい AST optimizerにかわりました。これにより、性能最適化がより一貫性のあるものになりました。

 

Constant folding has been moved from the peephole optimizer to the new AST optimizer, which is able perform optimizations more consistently. (Contributed by Eugene Toder and INADA Naoki in bpo-29469 and bpo-11549.)

 

参考: 定数畳み込み

定数畳み込み - Wikipedia

 


18.[性能向上] クラス生成や呼び出しの高速化と起動時間の短縮
 (クラス生成や呼び出しが1.5倍高速化、Python起動が最大10%短縮)

 

abcに含まれる主なファンクションやメソッドは(より高速な)C言語で書き換えられました。それによって、isinstance() と issubclass()の呼び出しは1.5倍ほど高速になりました。さらに、Pythonの起動時間も最大10%短縮されました。

 

Most functions and methods in abc have been rewritten in C. This makes creation of abstract base classes, and calling isinstance() and issubclass() on them 1.5x faster. This also reduces Python start-up time by up to 10%. (Contributed by Ivan Levkivskyi and INADA Naoki in bpo-31333)

 

 

19.[速度向上] datetime.date と datetime.datetimeのインスタンス生成速度の目覚ましい高速化

 

datetime.date と datetime.datetimeは、新たなコントラクタ(インスタンス生成機能)により処理速度が目覚ましく向上しました。これは、サブクラスを生成しない場合に、fast-pathコントラクタを使用することによって実現しました。

 

Significant speed improvements to alternate constructors for datetime.date and datetime.datetime by using fast-path constructors when not constructing subclasses. (Contributed by Paul Ganssle in bpo-32403)

 


20.[速度向上] array.arrayインスタンスの比較処理の高速化(10倍から70倍の高速化)

 

array.arrayインタンスの比較は、特定のケースでかなり処理速度が向上しました。
配列が保持する値が同じinteger型の場合は、10倍から70倍高速化しました。

 

The speed of comparison of array.array instances has been improved considerably in certain cases. It is now from 10x to 70x faster when comparing arrays holding values of the same integer type. (Contributed by Adrian Wielgosik in bpo-24700.)

 


21.[速度向上] math.erf() と math.erfc() functionsの高速化(高速なCライブラリにより実装)

 

math.erf() と math.erfc() functionsは、主なプラットフォーム上で、より高速なCライブラリで実装されました。

 

The math.erf() and math.erfc() functions now use the (faster) C library implementation on most platforms. (Contributed by Serhiy Storchaka in bpo-26121.)

 

今日はここまで!