Help us understand the problem. What is going on with this article?

pythonで並列処理のサンプル

単純に複数CPUを使ってみる

ALL about Linux: お手軽なベンチマークあれこれ

$ time for ((i=0;++i<1000000;)); do :; done

real    0m4.698s
user    0m4.663s
sys     0m0.000s
a.py
import multiprocessing
import time
import subprocess

class CmdProc(multiprocessing.Process):
    def __init__(self, number):
        super().__init__()
        self.number = number

    def run(self):
        subprocess.run("for ((i=0;++i<{};)); do :; done".format(self.number), shell=True)
        return


def main():
    procs = []
    for number in [1000000, 1000000]: # 上記コマンドを2個並列実行
        proc = CmdProc(number)
        proc.start()
        procs.append(proc)

    for proc in procs:
        proc.join()


if __name__ == '__main__':
    main()

起動コストからしてこれぐらいで妥当

$ time python3 a.py

real    0m4.909s
user    0m9.416s
sys     0m0.012s
b.py
import multiprocessing
import subprocess
from multiprocessing import Pool


def cmd_proc(number):
    subprocess.run("for ((i=0;++i<{};)); do :; done".format(number), shell=True)

def main():
    n_cores = multiprocessing.cpu_count()
    p = Pool(n_cores)
    res = p.map(cmd_proc, [1000000, 1000000])

if __name__ == '__main__':
    main()
$ time python3 b.py

real    0m4.894s
user    0m9.398s
sys     0m0.029s

Pythonの並列処理・並行処理をしっかり調べてみた - Qiita

k8uwall
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした