日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Parallel 并发编程实例

發布時間:2025/6/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Parallel 并发编程实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

算法計算小于數的所有? 素數和

不用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"
運行結果 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?并發編程

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()
運行結果

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



總結

以上是生活随笔為你收集整理的Parallel 并发编程实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲天堂一区二区在线观看 | 美日韩黄色片 | 米奇av| 先锋资源av网 | 天天想你在线观看完整版电影免费 | 黄色自拍视频 | 亚洲精品福利网站 | www.久久成人| 伦理av在线 | 狠狠干夜夜 | 久久久久久98 | 天天cao在线 | 亚洲天堂成人在线观看 | 欧美激情黄色 | 亚洲精品乱码久久久久久国产主播 | 免费看国产黄色片 | www.白丝 | 欧美在线观看视频一区 | 狠狠爱天天干 | 日韩国产欧美在线视频 | 国产精品日韩精品欧美精品 | 电家庭影院午夜 | 91看片免费版 | 日韩av毛片在线观看 | 能看av的网站 | 国产无套内射普通话对白 | 深夜视频在线观看免费 | 挪威xxxx性hd极品 | 超碰97在线资源 | 巨茎大战刘亦菲 | 亚洲成人精品一区二区 | 少妇愉情理伦片bd | 国产精品av在线免费观看 | 成人无码一区二区三区 | 午夜精品久久久久久久99热黄桃 | 亚洲综合第一 | 国产精品久久在线 | 搡老熟女国产 | 国产乱人乱偷精品视频a人人澡 | 老太太av | 九九热视频在线 | 欧美骚视频 | 99热这里只 | 亚洲第一黄色片 | 成人免费一级视频 | 五月婷婷视频在线观看 | 日韩a级片| 先锋av资源网| 四川一级毛毛片 | 女优视频在线观看 | 美国一级大黄一片免费中文 | 亚洲欧美日本韩国 | 久久久久夜夜夜精品国产 | 国产小视频网址 | 99re国产精品 | 成人特级毛片 | 久久精品国产99久久不卡 | 男人爽女人下面动态图 | 欧美日韩成人在线观看 | 欧美日韩中文视频 | 久久久久久黄色片 | 人妻与黑人一区二区三区 | 成人性生交生交视频 | 午夜网站在线 | 成人欧美一区二区三区黑人免费 | 成人在线天堂 | 五月婷婷狠狠爱 | 巨乳美女动漫 | 欧美中日韩在线 | 久久久欧美 | 韩国美女啪啪 | 欧美3p视频 | 草草在线观看 | 午夜精品久久久久久毛片 | 北条麻妃久久精品 | 香蕉免费在线视频 | 天天干妹子 | 色一情一乱一区二区三区 | 欧美一级黄色片 | 欧美日韩亚洲在线观看 | 国产精品高潮呻吟久久av野狼 | 日韩大片免费看 | 嫩草视频网站 | 综合在线亚洲 | 秋霞精品一区二区三区 | 欧美一区二区精品 | 女人下边被添全过视频 | 视频在线a | 精品久久久久久久久久久aⅴ | caopeng在线视频 | 成人免费高清在线观看 | 91天堂在线观看 | www.rihan | 精品视频一区二区在线观看 | 天堂91| 国产靠逼视频 | 成年人免费看黄色 | 天天干天天操天天插 | 色哟哟一区二区三区四区 |