Python多处理需要更多时间

编程入门 行业动态 更新时间:2024-10-24 09:26:38
本文介绍了Python多处理需要更多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有12核和28GB RAM的服务器.我正在运行两个版本的Python.一个具有多重处理功能,另一个具有顺序功能.与Sequential.py相比,我希望Multiprocessing.py能够更早完成,但是与顺序代码(25sec)相比,多处理代码(120sec)要花5倍的时间

I have server with 12 cores and 28GB RAM. I am running two versions of Python; one with multiprocessing and another sequential. I expect the Multiprocessing.py to finish early compared to Sequential.py but the multiprocessing code takes 5 times more (120sec) compared to sequential code (25sec)

Multiprocessing.py

Multiprocessing.py

import os,multiprocessing,time def cube(x): print(x**3) return if __name__ == '__main__': jobs = [] start = time.time() for i in range(5000): p = multiprocessing.Process(target=cube(i)) jobs.append(p) p.start() end = time.time() print end - start

Sequential.py

Sequential.py

import os,time def cube(x): print(x**3) return if __name__ == '__main__': start = time.time() for i in range(5000): cube(i) end = time.time() print end - start

可以请您帮忙吗?

推荐答案

问题是相对于IPC通信开销而言,要做的工作太少了.

The problem is that too little work is being done relative to the IPC communication overhead.

cube 函数不是多处理加速的理想选择.尝试一些更有趣"的函数,例如计算1到n的立方和的函数或类似的函数:

The cube function isn't a good candidate for multiprocessing speedup. Try something "more interesting" like function that computes the sum of cube for 1 to n or somesuch:

import os, multiprocessing, time def sum_of_cubes(n): return sum(x**3 for x in range(n)) if __name__ == '__main__': from multiprocessing.pool import ThreadPool as Pool pool = Pool(25) start = time.time() print(pool.map(sum_of_cubes, range(1000, 100000, 1000))) end = time.time() print(end - start)

一般规则是:

  • 启动的池不要超过您的核心可以受益的
  • 不要传递大量数据或返回大量数据(IPC负载过多)
  • 相对于IPC开销,在此过程中做了大量工作.

更多推荐

Python多处理需要更多时间

本文发布于:2023-11-26 20:07:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635004.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多处   时间   更多   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!