为什么这个异步矩阵乘法代码比同步慢?
我试图通过使用并发.futures.threadPoolExecutor() 线程来实现比同步乘法函数更快的矩阵乘法函数。代码如下:
def asyncmult(m1, m2):
threads = []
prod = np.zeros((m1.shape[0], m2.shape[1]))
def multvecs(inp):
vec1, vec2, index = inp
i, j = index
sum = 0
for k in range(vec1.shape[0]):
sum += vec1[k] * vec2[k]
prod[i][j] = sum
with concurrent.futures.ThreadPoolExecutor() as executor:
for i in range(m1.shape[0]):
for j in range(m2.shape[1]):
f = executor.submit(multvecs, (m1[i], m2[:, j], (i, j)))
threads.append(f)
return prod
在绘制输入矩阵大小增加所花费的时间时,该函数的性能比同步函数的性能更差。 为什么会发生这种情况?我该如何解决这个问题?
I'm trying to implement a matrix multiplication function that is faster than my synchronous multiplication function by using concurrent.futures.threadPoolExecutor() threads. Here is the code:
def asyncmult(m1, m2):
threads = []
prod = np.zeros((m1.shape[0], m2.shape[1]))
def multvecs(inp):
vec1, vec2, index = inp
i, j = index
sum = 0
for k in range(vec1.shape[0]):
sum += vec1[k] * vec2[k]
prod[i][j] = sum
with concurrent.futures.ThreadPoolExecutor() as executor:
for i in range(m1.shape[0]):
for j in range(m2.shape[1]):
f = executor.submit(multvecs, (m1[i], m2[:, j], (i, j)))
threads.append(f)
return prod
on plotting against time taken with increase in input matrix size this function performs the worse than synchronous.
why is this happening? How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论