这是我的并行处理代码,我比较了Python中相同任务的并行代码和线性代码的运行时。我得到的并行代码的输出时间是在每次运行更多。这完全出乎意料。为什么会这样?怎么了?你知道吗
from multiprocessing import Pool
import matplotlib.pyplot as plt
import numpy as np
import time
def scatter_join(args):
plt.scatter(s=1,*args)
plt.savefig('test_p.png')
if __name__ == '__main__':
p = Pool()
snap = np.random.randint(0,20,(100000,2))
task = [(snap[:,0],snap[:,1])]
t = time.time()
p.map(scatter_join,task)
p.close()
p.join()
print('Time Elapsed(Parallel): ', abs((time.time()-t)))
plt.clf()
t = time.time()
plt.scatter(snap[:,0],snap[:,1],s=1)
plt.savefig('test_l.png')
print('Time Elapsed(Linear): ',abs(time.time()-t))
并行代码不能保证比单个线程快。它可以加速或减慢工作负载。你知道吗
如果一段代码的串行(即不可并行)部分占了太多的百分比,那么并行化算法就不会有什么好处。你知道吗
从这里看起来,您的工作负载(单个matplotlib plot?)不会从并行执行中受益。你知道吗
也可以看看这个:
Python Multiprocessing with Matplotlib
输出为
运行时间(并行):1.8148565292358398
所用时间(线性):0.171877861022942
相关问题 更多 >
编程相关推荐