并行计算 python_Parallel Python(PP)并行计算测试

测试环境:i5-2300(4核) + Win7

使用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测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值