2014年8月7日木曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の12章(スレッド: 並列の世界)、長いエクササイズ(p.516)をpythonで考えてみる。

長いエクササイズ(p.516)

コード(BBEdit, Emacs)

sample516_a.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import sys
import threading

threads = []
beers = 2000000
lock = threading.Lock()

def error(msg):
    print("{0}: {1}".format(msg, err), file=sys.stderr)
    sys.exit(1)

def drink_lots():
    global beers
    lock.acquire()
    for n in range(100000):
        beers -= 1
    lock.release()
    print('beers =', beers)
    
print('壁にはビールが{0}本\n{0}本のビール'.format(beers))
for t in range(20):
    thread = threading.Thread(target=drink_lots)
    try:
        thread.start()
    except Exception as err:
        error(err)
    threads.append(thread)

for t in threads:
    try:
        t.join()
    except Exception as err:
        error(err)

print('現在、壁にはビールが{0}本あります。'.format(beers))

sample516_b.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import sys
import threading

threads = []
beers = 2000000
lock = threading.Lock()

def error(msg):
    print("{0}: {1}".format(msg, err), file=sys.stderr)
    sys.exit(1)

def drink_lots():
    global beers
    for n in range(100000):
        lock.acquire()
        beers -= 1
        lock.release()
    print('beers =', beers)
    
print('壁にはビールが{0}本\n{0}本のビール'.format(beers))
for t in range(20):
    thread = threading.Thread(target=drink_lots)
    try:
        thread.start()
    except Exception as err:
        error(err)
    threads.append(thread)

for t in threads:
    try:
        t.join()
    except Exception as err:
        error(err)

print('現在、壁にはビールが{0}本あります。'.format(beers))

入出力結果(Terminal, IPython)

$ ./sample516_a.py
壁にはビールが2000000本
2000000本のビール
beers = 1900000
beers = 1800000
beers = 1700000
beers = 1600000
beers = 1500000
beers = 1400000
beers = 1300000
beers = 1200000
beers = 1100000
beers = 1000000
beers = 900000
beers = 800000
beers = 700000
beers = 600000
beers = 500000
beers = 400000
beers = 300000
beers = 200000
beers = 100000
beers = 0
現在、壁にはビールが0本あります。
$ ./sample516_b.py
壁にはビールが2000000本
2000000本のビール
beers = 1300579
beers = 819749
beers = 674229
beers = 454050
beers = 362911
beers = 355916
beers = 299044
beers = 196702
beers = 177823
beers = 157745
beers = 75382
beers = 72627
beers = 70823
beers = 63248
beers = 49209
beers = 31814
beers = 10105
beers = 0
beers = 844
beers = 21858
現在、壁にはビールが0本あります。
$ time ./sample516_a.py
壁にはビールが2000000本
2000000本のビール
beers = 1900000
beers = 1800000
beers = 1700000
beers = 1600000
beers = 1500000
beers = 1400000
beers = 1300000
beers = 1200000
beers = 1100000
beers = 1000000
beers = 900000
beers = 800000
beers = 700000
beers = 600000
beers = 500000
beers = 400000
beers = 300000
beers = 200000
beers = 100000
beers = 0
現在、壁にはビールが0本あります。

real 0m0.347s
user 0m0.329s
sys 0m0.019s
$ time ./sample516_b.py
壁にはビールが2000000本
2000000本のビール
beers = 1162786
beers = 472821
beers = 444107
beers = 434637
beers = 430698
beers = 379902
beers = 379420
beers = 301852
beers = 252605
beers = 179233
beers = 175693
beers = 151523
beers = 148012
beers = 107366
beers = 73565
beers = 66355
beers = 42860
beers = 32839
beers = 56482
beers = 0
現在、壁にはビールが0本あります。

real 0m19.568s
user 0m6.780s
sys 0m22.330s
$

0 コメント:

コメントを投稿