为什么Python中的并行处理比串行处理慢?
#!/usr/bin/env python3
import os
import time
from functools import partial
import multiprocessing as mp
def print_hello(i, typ):
print(typ + " : PID-" + str(os.getpid()) + "\n")
# print("Hello World " + str(i))
if __name__ == '__main__':
N= mp.cpu_count()
parallel_start_time = time.time()
with mp.Pool(processes = N) as p:
p.map(partial(print_hello,typ="Parallel"), (x for x in range(100)))
parallel_end_time = time.time()
serial_start_time = time.time()
for x in range(100):
print_hello(x, "Serial")
serial_end_time = time.time()
print("Parallel processing took " + str(parallel_end_time - parallel_start_time) + " seconds")
print("Serial processing took " + str(serial_end_time - serial_start_time) + " seconds")
Run Code Online (Sandbox Code Playgroud)
我将上述脚本的输出写入文本文件,下面是最终输出
./test_parallel.py > pid.txt
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)61 Parallel : PID-28311 Parallel processing took 0.11675715446472168 seconds 100 Serial : PID-28310 Serial processing took 0.0001430511474609375 seconds
我也不明白为什么python在使用并行处理时不处理39个id
小智 5
在多处理中,您还需要考虑创建进程的开销。在这个例子中,每个进程都在执行一个非常小的任务,即打印一条语句。因此,开销将相当大,这就是您得到这样的结果的原因。
建议在CPU 密集型(计算量大)任务中使用多重处理。但是,这里的情况并非如此。因此,您应该在其他示例上尝试一下。
| 归档时间: |
|
| 查看次数: |
238 次 |
| 最近记录: |