Parallel 并发编程实例
生活随笔
收集整理的這篇文章主要介紹了
Parallel 并发编程实例
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
算法計算小于數(shù)的所有? 素數(shù)和
不用Parallel
# sum_primes_without_pp.py import 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() start=time.clock() inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700,676899,646899,677899,676099,675899,646899,677099,606099) for input in inputs:print "Sum of primes below", input, "is", sum_primes(input)elapsed = (time.clock() - start) print "Time elapsed: ", elapsed, "s" print "Time elapsed: ", time.time() - start_time, "s"運行結(jié)果 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 Sum of primes below 676899 is 17725655031 Sum of primes below 646899 is 16245807217 Sum of primes below 677899 is 17773746096 Sum of primes below 676099 is 17690477739 Sum of primes below 675899 is 17679661703 Sum of primes below 646899 is 16245807217 Sum of primes below 677099 is 17739194685 Sum of primes below 606099 is 14352855223 Time elapsed: 100.017748574 s Time elapsed: 100.016000032 s
用Parallel?并發(fā)編程 import math, sys, time import 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 with ppservers = () #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 available job1 = 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 available result = job1()print "Sum of primes below 100 is", resultstart_time = time.time()# The following submits 8 jobs and then retrieves the results inputs = (100000, 100100, 100200, 100300, 100400, 100500, 100600, 100700,676899,646899,677899,676099,675899,646899,677099,606099) 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()
運行結(jié)果 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 systemStarting pp with 4 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 Sum of primes below 676899 is 17725655031 Sum of primes below 646899 is 16245807217 Sum of primes below 677899 is 17773746096 Sum of primes below 676099 is 17690477739 Sum of primes below 675899 is 17679661703 Sum of primes below 646899 is 16245807217 Sum of primes below 677099 is 17739194685 Sum of primes below 606099 is 14352855223 Time elapsed: 39.5499999523 s Job execution statistics:job count | % of all jobs | job time sum | time per job | job server17 | 100.00 | 154.4120 | 9.083059 | local Time elapsed since server creation 39.5599999428 0 active tasks, 4 cores
總結(jié)
以上是生活随笔為你收集整理的Parallel 并发编程实例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python串行运算、并行运算、多线程、
- 下一篇: 《Java解惑》陷阱和缺陷的目录