コンパイラPython:Pypyの実力

久ぶりにPythonの話題である。

インタプリタであるPythonは実行速度が遅い。この遅い原因を回避するためにPython言語で作ったプログラムをコンパイルして実行するPypyというコンパイラがある。どの位の性能があるか試してみた。環境はOSはwindows7で、使ったPythonおよびPypyのヴァージョンは
>>python -V
Python 3.7.6

>>pypy3 -V
Python 3.6.9 (2ad108f17bdb, Apr 07 2020, 03:05:35)
[PyPy 7.3.1 with MSC v.1912 32 bit]

例1:単純な四則計算


import time
start = time.time()

for i in range(100000000): # 10^8
    1 + 1
    1 - 1
    1 * 1
    1 // 1

process_time = time.time() - start
print(process_time)

結果は
Python3.7  4159ms
Pypy  95ms

となり、約40倍の速度向上が見られた。

例2:配列


import time
start = time.time()

A = [i for i in range(10000000)] # 10^7

for i in range(10000000):
    A[i] = 0

process_time = time.time() - start
print(process_time)

結果は
Python3.7  1844ms
Pypy  70ms

となり、約30倍の速度向上が見られた。