我多次运行一个函数,该函数只包含一个计数为一亿的简单for循环。
使用时间模块,我可以跟踪程序同步和异步(使用多处理(完成计算所需的时间。我预计这两次会有很大的不同,但我的结果与我的预期相反。有人能帮我理解我做错了什么吗?
提前感谢
import time
import concurrent.futures
start = time.perf_counter()
def new_func():
for _ in range(100_000_000):
count = 0
count += 1
return print("Finished Processing")
new_func()
new_func()
new_func()
new_func()
new_func()
time1 = time.perf_counter()
normal_time = time1 - start
print(f"the normal time is: {normal_time}")
with concurrent.futures.ProcessPoolExecutor() as executor:
[executor.submit(new_func()) for _ in range(5)]
time2 = time.perf_counter()
multi_time = time2-time1
print(f"the multi-time is {multi_time}")
这是这个代码的结果
[Running] python -u "/home/emeraude/tutorial/testing.py"
Finished
Finished
Finished
Finished
Finished
the normal time is: 19.87812520000034
Finished
Finished
Finished
Finished
Finished
the multi-time is 18.957598699999835
[Done] exited with code=0 in 38.869 seconds
[executor.submit(new_func()) for _ in range(5)]
您没有提交函数-您提交的是"无"。为什么?您可以在submit中执行new_func()
,它会立即运行函数。
取而代之的是
[executor.submit(new_func) for _ in range(5)]
(另外,不要将return
作为print()
-该函数也返回None(
问题是这个执行不是并行的:
with concurrent.futures.ProcessPoolExecutor() as executor:
[executor.submit(new_func()) for _ in range(5)]
您正在创建一个包含五个元素的列表,这是执行new_func
的结果。
如果你想并行执行,我建议你以下替代方案:
def new_func(dummy=None):
for _ in range(100_000_000):
count = 0
count += 1
return "Finished Processing"
with concurrent.futures.ProcessPoolExecutor() as executor:
for e in executor.map(new_func, [i for i in range(5)]):
print(e)
所以输出:
❯ python foo.py 0s
Date: 2021-07-01 Time: 11:37:27
Finished Processing
Finished Processing
Finished Processing
Finished Processing
Finished Processing
the normal time is: 14.901674957000068
Finished Processing
Finished Processing
Finished Processing
Finished Processing
Finished Processing
the multi-time is 3.9148486799995226