您的位置:首页 > 编程语言 > Python开发

Parallel Python(PP)并行计算测试

2016-03-16 14:44 666 查看
测试环境:i5-2300(4核) + Win7

测试任务还是之前用的takeuptime()函数,串行计算实验的结果可以看这里:http://www.redicecn.com/html/Python/20111223/355.html

使用PP的测试代码如下:

import math, sys, time
import pp

def takeuptime(n):
chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
s = chars * 1000
for i in range(10*n):
for c in chars:
s.count(c)

print """Usage: test.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 with
ppservers = ()
#ppservers = ("10.0.0.1",)

if len(sys.argv) > 1:
ncpus = int(sys.argv[1])
# Creates jobserver with ncpus workers
job_server = pp.Server(ncpus, ppservers=ppservers)
else:
# Creates jobserver with automatically detected number of workers
job_server = pp.Server(ppservers=ppservers)

print "Starting pp with", job_server.get_ncpus(), "workers"

start_time = time.time()

# The following submits 4 jobs
inputs = (1000, 1000, 1000, 1000)
jobs = [(input, job_server.submit(takeuptime, (input,), (), ())) for input in inputs]

#wait for jobs in all groups to finish
job_server.wait()

print "Time elapsed: ", time.time() - start_time, "s"
job_server.print_stats()


程序运行结果如下:

I:\Webscraping\test>test

Usage: test.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

Starting pp with 4 workers

Time elapsed: 20.9220001698 s

Job execution statistics:

job count | % of all jobs | job time sum | time per job | job server

4 | 100.00 | 78.6590 | 19.664750 | local

Time elapsed since server creation 20.9240000248

所需的时间和之前用pprocess模块进行并行运算的结果差不多。

PP与pprocess模块相比优势在哪里?

1)PP不但支持Linux,Windows下也能使用。

2)PP不但支持单机多核(SMP,systems with multiple processors or cores),而且支持多台计算机(clusters,computers connected via network)。

目前只测试了SMP,期待clusters测试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: