设为首页 收藏本站
查看: 373|回复: 0

[经验分享] 测试python的并行模块Parallel Python的效率

[复制链接]

尚未签到

发表于 2017-4-27 12:00:22 | 显示全部楼层 |阅读模式
  未使用pp模块的代码执行时间测试:
  下面的代码计算小于等于n的质数之和
# sum_primes_without_pp.pyimport math, sys, timedef isprime(n):"""Returns True if n is prime and False otherwise"""if not isinstance(n, int):raise TypeError("argument passed to is_prime is not of 'int' type")if n < 2:return Falseif n == 2:return Truemax = int(math.ceil(math.sqrt(n)))i = 2while i <= max:if n % i == 0:return Falsei += 1return Truedef sum_primes(n):"""Calculates sum of all primes below given integer n"""return sum([x for x in xrange(2,n) if isprime(x)])start_time = time.time()inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)for input in inputs:print "Sum of primes below", input, "is", sum_primes(input)print "Time elapsed: ", time.time() - start_time, "s"  
测试结果:
  Sum of primes below 100000 is 454396537
Sum of primes below 100100 is 454996777
Sum of primes below 100200 is 455898156
Sum of primes below 100300 is 456700218
Sum of primes below 100400 is 457603451
Sum of primes below 100500 is 458407033
Sum of primes below 100600 is 459412387
Sum of primes below 100700 is 460217613
Time elapsed: 3.95300006866 s

  下面的代码使用并行模块实现:
#!/usr/bin/python# File: sum_primes.py# Author: VItalii Vanovschi# Desc: This program demonstrates parallel computations with pp module# It calculates the sum of prime numbers below a given integer in parallel# Parallel Python Software: http://www.parallelpython.comimport math, sys, timeimport ppdef isprime(n):"""Returns True if n is prime and False otherwise"""if not isinstance(n, int):raise TypeError("argument passed to is_prime is not of 'int' type")if n < 2:return Falseif n == 2:return Truemax = int(math.ceil(math.sqrt(n)))i = 2while i <= max:if n % i == 0:return Falsei += 1return Truedef sum_primes(n):"""Calculates sum of all primes below given integer n"""return sum([x for x in xrange(2,n) if isprime(x)])print """Usage: python sum_primes.py [ncpus][ncpus] - the number of workers to run in parallel, if omitted it will be set to the number of processors in the system"""# tuple of all parallel python servers to connect withppservers = ()#ppservers = ("10.0.0.1",)if len(sys.argv) > 1:ncpus = int(sys.argv[1])# Creates jobserver with ncpus workersjob_server = pp.Server(ncpus, ppservers=ppservers)else:# Creates jobserver with automatically detected number of workersjob_server = pp.Server(ppservers=ppservers)print "Starting pp with", job_server.get_ncpus(), "workers"# Submit a job of calulating sum_primes(100) for execution. # sum_primes - the function# (100,) - tuple with arguments for sum_primes# (isprime,) - tuple with functions on which function sum_primes depends# ("math",) - tuple with module names which must be imported before sum_primes execution# Execution starts as soon as one of the workers will become availablejob1 = job_server.submit(sum_primes, (100,), (isprime,), ("math",))# Retrieves the result calculated by job1# The value of job1() is the same as sum_primes(100)# If the job has not been finished yet, execution will wait here until result is availableresult = job1()print "Sum of primes below 100 is", resultstart_time = time.time()# The following submits 8 jobs and then retrieves the resultsinputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700)jobs = [(input, job_server.submit(sum_primes,(input,), (isprime,), ("math",))) for input in inputs]for input, job in jobs:print "Sum of primes below", input, "is", job()print "Time elapsed: ", time.time() - start_time, "s"job_server.print_stats()  
测试结果如下:
  Starting pp with 2 workers
Sum of primes below 100 is 1060
Sum of primes below 100000 is 454396537
Sum of primes below 100100 is 454996777
Sum of primes below 100200 is 455898156
Sum of primes below 100300 is 456700218
Sum of primes below 100400 is 457603451
Sum of primes below 100500 is 458407033
Sum of primes below 100600 is 459412387
Sum of primes below 100700 is 460217613
Time elapsed: 1.95300006866 s
Job execution statistics:
job count | % of all jobs | job time sum | time per job | job server
9 | 100.00 | 3.9060 | 0.434000 | local
Time elapsed since server creation 1.95300006866
可以发现同样的任务,节约的时间很多,效率提高100%!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-369982-1-1.html 上篇帖子: python教程:几行代码搞定python 设计模式 下篇帖子: Python基础 7 ---- Python内置sort和sorted函数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表