7
3

AmazonLinux2にPython3.13.0rc1をインストールしてThreadの性能を比較してみた

Last updated at Posted at 2024-08-04

8月1日にPython3.13.0rc1がダウンロード可能になりましたので、AmazonLinux2に入れ簡易スクリプト使ってThreadingの性能を3.9.19と比較してみました。リリース内容は以下のページから確認できます。

前提

OS:Linux amazonlinux2 4.14.348-265.565

1. 必要packageのインストール

ビルドに必要となるpackageを入れておきます。

yum install -y bzip2 bzip2-devel gcc git libffi-devel openssl11 openssl11-devel readline readline-devel sqlite sqlite-devel zlib-devel libdb-devel gdbm-devel xz-devel tk-devel uuid-devel

公式サイトからPython-3.13.0rc1.tgzをダウンロードしてインストールしましょう。

wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0rc1.tgz
tar xzvf Python-3.13.0rc1.tgz
cd Python-3.13.0rc1
./configure --enable-optimizations
make
make install

2. Thread性能比較

それでは、スレッドの負荷を計測したいと思います。スレッド本数は10, 100を試してみました。また、起動された各スレッドそれぞれは1,000,000回の、平方計算を実施してCPU負荷を与えるようにしてみました。

import threading
import time
from datetime import datetime

def worker(thread_id):
    start_time = datetime.now()
    print(f"Thread {thread_id} starting at {start_time}")

    result = 0
    for i in range(10**6):
        result += i ** 2

    end_time = datetime.now()
    print(f"Thread {thread_id} finished at {end_time}, duration: {end_time - start_time}, result: {result}")

def main():
    threads = []
    num_threads = 10 # 10, 100をそれぞれ実施。

    for i in range(num_threads):
        thread = threading.Thread(target=worker, args=(i,))
        threads.append(thread)
        thread.start()

    for thread in threads:
        thread.join()

    finished_time = datetime.now()
    print(f"finished time is {finished_time}")
    print("All threads finished")

if __name__ == "__main__":
    main()

同一スペックのサーバにPython3.9.19を準備して比較した結果となります。

Thread本数 Python3.9.19 Python3.13
10 約2秒 1秒未満
100 約16秒 約4秒

かなり改善されているのがわかります。

コマンド実行時(スレッド100本)のリソース状況です。

Python3.13
[root@amazonlinux ~]# while :; do ps -aux | grep python3 | grep -v grep ; sleep 1; done
root      92238  0.0  0.2 726048  9696 pts/0    Sl+  10:04   0:00 python3 test.py
root      92238 68.0  0.2 1250512 9776 pts/0    Sl+  10:04   0:01 python3 test.py
root      92238 79.3  0.2 1266936 10172 pts/0   Sl+  10:04   0:02 python3 test.py
root      92238 84.7  0.2 1349072 10208 pts/0   Sl+  10:04   0:03 python3 test.py
Python3.9
[root@amazonlinux ~]# while :; do ps -aux | grep python3 | grep -v grep ; sleep 1; done
root      40914 43.0  0.2 655528  8752 pts/1    Sl+  10:05   0:00 python3 test.py
root      40914 71.5  0.2 1253836 8848 pts/1    Rl+  10:05   0:01 python3 test.py
root      40914 81.3  0.2 1286620 8864 pts/1    Sl+  10:05   0:02 python3 test.py
root      40914 86.0  0.2 1303012 8880 pts/1    Sl+  10:05   0:03 python3 test.py
root      40914 89.0  0.2 1303012 8952 pts/1    Sl+  10:05   0:04 python3 test.py
root      40914 91.0  0.2 1303012 9360 pts/1    Sl+  10:05   0:05 python3 test.py
root      40914 92.4  0.2 1294816 9376 pts/1    Sl+  10:05   0:06 python3 test.py
root      40914 93.5  0.2 1294816 9388 pts/1    Sl+  10:05   0:07 python3 test.py
root      40914 94.2  0.2 1311208 9400 pts/1    Sl+  10:05   0:08 python3 test.py
root      40914 94.9  0.2 1327600 9404 pts/1    Sl+  10:05   0:09 python3 test.py
root      40914 95.4  0.2 1360384 9416 pts/1    Sl+  10:05   0:10 python3 test.py
root      40914 95.9  0.2 1393168 9448 pts/1    Sl+  10:05   0:11 python3 test.py
root      40914 96.3  0.2 1401364 9448 pts/1    Sl+  10:05   0:12 python3 test.py
root      40914 96.6  0.2 1417756 9716 pts/1    Sl+  10:05   0:13 python3 test.py
root      40914 96.9  0.2 1376776 9684 pts/1    Sl+  10:05   0:14 python3 test.py
root      40914 97.1  0.2 1327600 9616 pts/1    Sl+  10:05   0:15 python3 test.py

その他

3.15/3.16から削除予定の機能は非推奨になっているようです。移行の際は以下のページを確認しておいた方が良さそうです。

AmazonLinux2ではSqlite、tkのバージョンが古いため、利用する場合はSourceからインストールする必要がありそうです。※AmazonLinux2023を利用したほうが早いですね。

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