LoginSignup
8
5

More than 5 years have passed since last update.

Python3とPyPy3のパフォーマンスを比較してみた

Last updated at Posted at 2018-04-28

最近、Pythonの勉強を始めた。
その中で、Pythonのパフォーマンスを向上させる実装としてPyPyというのを知ったのだが、インフラエンジニア的にはパフォーマンスは気になるので、実験してみた。

環境

マシン: Frontier FRNLC380/KD3
CPU: Celeron 3855U 1.60GHz
Mem: DDR3L-1600 8GB X1
OS: Fedora 27
Python version:Python 3.6.5
Pypy version: Python 3.3.5 (19b4b275ee9c, Aug 04 2017, 13:10:56)
[PyPy 5.5.0-alpha0 with GCC 7.1.1 20170718 (Red Hat 7.1.1-6)]

テスト用コード

test.py
import math
MAX=10000000
NUM=2
while NUM <= MAX:
    LIMIT=int(math.sqrt(NUM))
    i=1
    while i <= LIMIT:
        i += 1
        if i == NUM:
            FLAG = False
            print(NUM)
            NUM = 1
            continue
        remainder = NUM % i
        if remainder == 0:
            FLAG = False
            break
        else:
            FLAG = True
    if (FLAG):
        print(NUM)
    NUM += 2

10,000,000までの素数をひたすら求めるプログラム。

結果

PyPy: 53秒(!)
CPython: 13分8秒

$ date; pypy3 test.py>/dev/null; date; python3 test.py>/dev/null; date
2018年  4月 28日 土曜日 11:40:20 JST <= PyPyスタート
2018年  4月 28日 土曜日 11:41:13 JST <= PyPy終了 & CPythonスタート
2018年  4月 28日 土曜日 11:54:21 JST <= CPython終了

その差、1486.79%!
ほぼ15倍速!

PyPy, すごすぎ!

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5