In [1]:
import multiprocessing
import time

멀티쓰레드 미사용시 시간


In [6]:
%%time
start_time = time.time()

def count(name):
    for i in range(1, 30000001):
        if (i%10000000)==0:
            print(name, " : ", i)
            
num_list = ['p1','p2','p3','p4']

for num in num_list:
    count(num)
    
print("===={} seconds====".format(time.time()-start_time))


p1  :  10000000
p1  :  20000000
p1  :  30000000
p2  :  10000000
p2  :  20000000
p2  :  30000000
p3  :  10000000
p3  :  20000000
p3  :  30000000
p4  :  10000000
p4  :  20000000
p4  :  30000000
====8.327701091766357 seconds====
CPU times: user 8.3 s, sys: 21.3 ms, total: 8.32 s
Wall time: 8.33 s

In [8]:
%%time
start_time = time.time()

def count(name):
    for i in range(1, 30000001):
        if (i%10000000)==0:
            print(name, " : ", i)
            
num_list = ['p1','p2','p3','p4']

pool = multiprocessing.Pool(processes=4)
pool.map(count, num_list)
pool.close()
pool.join()
    
print("===={} seconds====".format(time.time()-start_time))


p2  :  10000000
p3  :  10000000
p4  :  10000000
p1  :  10000000
p2  :  20000000
p3  :  20000000
p4  :  20000000
p1  :  20000000
p2  :  30000000
p4  :  30000000
p3  :  30000000
p1  :  30000000
====2.744521141052246 seconds====
CPU times: user 17.5 ms, sys: 20.1 ms, total: 37.5 ms
Wall time: 2.75 s

In [ ]: