我想学习python中的多处理/线程,我写了一个简单的代码,如下所示。如何通过多处理/线程加速此代码。
代码非常简单。
my_numbers = {}
for i in range(0,my_number1):
my_numbers[i] = zeros(my_number2, dtype=int)
现在我只想为列表中的每个数字加 1:
for i in my_numbers:
my_numbers[i] += 1
如何使用多处理/线程来加快for loop
?
附言1:my_numbers = ones(my_number2,dtype=int)
不是我想要的吗?我正在尝试通过多处理for loop
来加快计算速度。
p.s. 2:我有 12 个 CPU 和 32GB 内存。
这是我想做的,但它比较慢。
from multiprocessing import Process, Manager
from numpy import *
import time
def f(d,i):
d[i] += i
if __name__ == '__main__':
manager = Manager()
t = time.time()
d = manager.dict()
for i in range(100):
d[i] = array([0,0])
k = d.keys()
while len(k) >= 4:
p1 = Process(target=f, args=(d,k[0],))
p2 = Process(target=f, args=(d,k[1],))
p3 = Process(target=f, args=(d,k[2],))
p4 = Process(target=f, args=(d,k[3],))
p1.start()
p2.start()
p3.start()
p4.start()
p1.join()
p2.join()
p3.join()
p4.join()
k = k[4:]
else:
if len(k) == 4:
p1 = Process(target=f, args=(d,k[0],))
p2 = Process(target=f, args=(d,k[1],))
p3 = Process(target=f, args=(d,k[2],))
p4 = Process(target=f, args=(d,k[3],))
p1.start()
p2.start()
p3.start()
p4.start()
p1.join()
p2.join()
p3.join()
elif len(k) == 3:
p1 = Process(target=f, args=(d,k[0],))
p2 = Process(target=f, args=(d,k[1],))
p3 = Process(target=f, args=(d,k[2],))
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
elif len(k) == 2:
p1 = Process(target=f, args=(d,k[0],))
p2 = Process(target=f, args=(d,k[1],))
p1.start()
p2.start()
p1.join()
p2.join()
if len(k) == 1:
d[k[0]] += k[0]
print time.time()-t
print d
tt = time.time()
d = {}
for i in range(100):
d[i] = array([0,0])
for i in d:
d[i] += i
print time.time()-tt
print d
如果有人可以提出改进建议,我将不胜感激。谢谢。