0
0

More than 3 years have passed since last update.

Python vs PyPy vs Pyston ...ファイッ

Posted at

はじめに

先日、"Pythonより約40%高速な「Pyston 2.1」が登場" というニュースを見た

「Pyston か〜、PyPy も早いらしいけどどんなもんなんやろか?」と思ったため簡単に検証

結論

[早い] PyPy >> Pyston > Python [遅い]

検証

環境構築

環境は Docker コンテナで行う

以下の Dockerfile をビルドして検証

FROM ubuntu:20.04
WORKDIR /usr/local/src
RUN apt update && apt -y install python3 pypy3 wget vim\
    && wget https://github.com/pyston/pyston/releases/download/v2.1/pyston_2.1_20.04.deb

これで Python, PyPy はインストールできるが肝心の Pyston に関しては apt install ./pyston_2.1_20.04.deb でインストールしようとすると途中でキー入力を求められるため Dockerfile でやるのは断念

それぞれのバージョンは以下の通り

# python3 -V
Python 3.8.5

# pypy3 -V
Python 3.6.9 (7.3.1+dfsg-4, Apr 22 2020, 05:15:29)
[PyPy 7.3.1 with GCC 9.3.0]

# pyston -V
Python 3.8.2 (heads/rel2.1:da378ef, Jan 12 2021, 15:46:12)
[Pyston 2.1.0, GCC 9.3.0]

フィボナッチ数列

こういった処理速度系の検証はフィボナッチ数列が定番だと勝手に思っているため以下のコードで検証を行う

from datetime import datetime

count: int = 0

def fib(n: int) -> int:
    global count
    count += 1

    if n == 0:
        return 0
    if n == 1:
        return 1
    return fib(n - 1) + fib(n - 2)


def main():
    n = 40
    start_time = datetime.now()
    fib(n)
    end_time = datetime.now()
    print(f'count: { count }')
    print(f'time: { end_time - start_time }')


if __name__ == '__main__':
    main()

結果

# python3 fib.py
count: 331160281
time: 0:00:38.562256

# pyston fib.py
count: 331160281
time: 0:00:26.015875

# pypy3 fib.py
count: 331160281
time: 0:00:02.939397

PyPy が早すぎる...

Pyston も Python に比べたら 30% くらい早くなっている

ただ、Pyston も PyPy も使えるサードパーティのライブラリには制限があるようなのでこの結果は一つの参考までになればと思います

0
0
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
0
0