Pool

 

 1 import time
 2 from multiprocessing import Pool
 3 
 4 def decorator_timer(old_func):
 5     def new_func(*arg,**dict_arg):
 6         t1 = time.time()
 7         result = old_func(*arg,**dict_arg)
 8         t2 = time.time()
 9         print('time:',t2-t1)
10         return result
11     return new_func
12 def sum_num(i,num = 10000000):
13     sum = 0
14     for j in range(num):
15         sum += j
16     return sum
17 @decorator_timer
18 def single_thread(f,counts):
19     result = map(f,range(counts))
20     return list(result)
21 @decorator_timer
22 def multiple_thread(f,counts,process_number = 10):
23     p = Pool(process_number)
24     return p.map(f,range(counts))
25 if __name__ == "__main__":
26     # @decorator_timer
27     # def multiple_thread(f, counts, process_number=10):
28     #     p = Pool(process_number)
29     #     return p.map(f, range(counts))
30     TOTAL = 10
31     print(single_thread(sum_num,TOTAL))
32     print(multiple_thread(sum_num, TOTAL))
View Code
time: 6.125459671020508
[49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000]
time: 6.391090154647827
[49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000]

TOTAL = 100

time: 5.9535698890686035
[49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000]
time: 5.766067743301392
[49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000, 49999995000000]
但卡了

 

posted @ 2020-05-31 20:18  凤鸣朝阳水龙吟  阅读(548)  评论(0)    收藏  举报