为什么 ThreadPoolExecutor 比 for 循环慢?
Why is ThreadPoolExecutor slower than for loop?
代码 1
def feedforward(self,d):
out = []
for neuron in self.layer:
out.append(neuron.feedforward(d))
return np.array(out)
这是我为执行前馈而编写的原始代码。我想使用多线程提高执行速度,所以我编辑了代码以使用 concurrent.futures
模块
中的 ThreadPoolExecutor
代码 2
def parallel_feedforward(self,func,param):
return func.feedforward(param)
def feedforward(self,d):
out = []
with ThreadPoolExecutor(max_workers = 4) as executor:
new_d = np.tile(d,(len(self.layer),1))
for o in executor.map(self.parallel_feedforward,self.layer,new_d):
out.append(o)
return np.array(out)
变量 d
是一个向量,我使用 np.tile()
以便 executor.map
正确获取输入
计算两者的执行速度后。我发现代码 1 比代码 2 快得多(代码 2 几乎慢 8-10 倍)。但是使用多线程的代码不会比循环对应的代码更快吗?是因为我写的代码错误还是因为其他原因。如果是因为我的代码中的某些错误,有人可以告诉我我做错了什么吗?
提前感谢您的帮助。
哈里,
你应该在 python 和线程上做一个快速的 google - 值得注意的是 python "threads" 不会 运行 并行,因为 python GIL (...google 它)。因此,如果您上面的函数是 CPU 绑定的,那么使用 python 线程实际上不会像上面那样 运行 更快。
要真正 运行 并行,您需要改用 ProcessPoolExecutor - 它绕过线程中存在的 python "GIL lock"。
至于为什么它可能 运行 8-10 倍 更慢 - 只有一个想法是,当你使用期货时,当你用参数调用一个执行者,futures 将挑选你的参数传递给工人,然后工人将在 thread/process 中取消腌制以在那里使用。 (如果这对您来说是新手,请快速 google 进行 python 酸洗)
如果您的参数非常大,这可能会花费大量时间。
这可能就是您看到速度放缓的原因。 ...我发现我自己的代码速度非常慢,因为我试图将大型参数传递给工作人员。
代码 1
def feedforward(self,d):
out = []
for neuron in self.layer:
out.append(neuron.feedforward(d))
return np.array(out)
这是我为执行前馈而编写的原始代码。我想使用多线程提高执行速度,所以我编辑了代码以使用 concurrent.futures
模块
ThreadPoolExecutor
代码 2
def parallel_feedforward(self,func,param):
return func.feedforward(param)
def feedforward(self,d):
out = []
with ThreadPoolExecutor(max_workers = 4) as executor:
new_d = np.tile(d,(len(self.layer),1))
for o in executor.map(self.parallel_feedforward,self.layer,new_d):
out.append(o)
return np.array(out)
变量 d
是一个向量,我使用 np.tile()
以便 executor.map
正确获取输入
计算两者的执行速度后。我发现代码 1 比代码 2 快得多(代码 2 几乎慢 8-10 倍)。但是使用多线程的代码不会比循环对应的代码更快吗?是因为我写的代码错误还是因为其他原因。如果是因为我的代码中的某些错误,有人可以告诉我我做错了什么吗?
提前感谢您的帮助。
哈里,
你应该在 python 和线程上做一个快速的 google - 值得注意的是 python "threads" 不会 运行 并行,因为 python GIL (...google 它)。因此,如果您上面的函数是 CPU 绑定的,那么使用 python 线程实际上不会像上面那样 运行 更快。
要真正 运行 并行,您需要改用 ProcessPoolExecutor - 它绕过线程中存在的 python "GIL lock"。
至于为什么它可能 运行 8-10 倍 更慢 - 只有一个想法是,当你使用期货时,当你用参数调用一个执行者,futures 将挑选你的参数传递给工人,然后工人将在 thread/process 中取消腌制以在那里使用。 (如果这对您来说是新手,请快速 google 进行 python 酸洗)
如果您的参数非常大,这可能会花费大量时间。
这可能就是您看到速度放缓的原因。 ...我发现我自己的代码速度非常慢,因为我试图将大型参数传递给工作人员。