LoginSignup
4
4

More than 5 years have passed since last update.

multiprocessing vs threading

Posted at

時間計測するため laptimerを用意する

parallel.py
#!/usr/bin/python

from multiprocessing import Process
from threading       import Thread

def thread(function, args, processes):

    process_list = []
    for i in xrange(processes):
        p = Thread(target=function, args=args)
        p.start()
        process_list.append(p)
    for i in process_list:
        i.join()

def multiprocess(function, args, processes):

    process_list = []
    for i in xrange(processes):
        p = Process(target=function, args=args)
        p.start()
        process_list.append(p)
    for i in process_list:
        i.join()

if __name__ == '__main__':

    def worker(x, y):
        for a in xrange(x):
            for b in xrange(y):
                c = a * b

    from laptimer import LapTimer

    timer = LapTimer("multiprocessing vs threading")

    # multiprocess
    multiprocess(worker, [100, 200], 10)
    timer.lap("multiprocess")

    # threading
    thread(worker, [100, 200], 10)
    timer.lap("threading")

    timer.get_lap()

結果

-------------------------
multiprocessing vs threading
   Lap : Total : message
-------------------------
 0.037 : 0.037 : multiprocess
 0.017 : 0.055 : threading
4
4
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
4
4