LoginSignup
0
1

More than 5 years have passed since last update.

めざせpythonライブラリマスター (34)concurrent

Last updated at Posted at 2016-06-05

【ライブラリ説明】

 並列実行

【プログラム】

concurrent.py
# -*- coding: utf-8 -*-

from benchit import BenchIt
from concurrent import futures

b = BenchIt()

def process_job(job):
    count = 10000000
    while count>0:
        count -= 1
    return job

jobs = [i for i in range(20)]

# スレッド無し
result = []
for job in jobs:
    result.append(process_job(job))

b.mark("Non-Thread")

# スレッド有り
result = []
thread_pool = futures.ThreadPoolExecutor(max_workers=5)
result = thread_pool.map(process_job, jobs)

b.mark("Multi-thread5")

# スレッド有り
result = []
thread_pool = futures.ThreadPoolExecutor(max_workers=10)
result = thread_pool.map(process_job, jobs)

b.mark("Multi-thread10")

b.display()

【結果】

display.text
+----------------+----------+------+-------+----------+----------+---------+
| Marker         | Method   | Line | Loops | Avg Time |  Runtime | Percent |
+----------------+----------+------+-------+----------+----------+---------+
| Non-Thread     | <module> |   21 |     1 | 14.82156 | 14.82156 |   88.72 |
| Multi-thread5  | <module> |   28 |     1 |  0.18812 |  0.18812 |    1.13 |
| Multi-thread10 | <module> |   35 |     1 |  1.69720 |  1.69720 |   10.16 |
+----------------+----------+------+-------+----------+----------+---------+
Total runtime: 16.71

【参考サイト】

 document
 めざせpythonライブラリマスター (27)benchit

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