Drkcore

12 01 2010 Python macbook Clojure Tweet

pythonでthreadingとmultiprocessing

Python vs Clojure – Evolving « Best in Classをみてて、「ほう」となったので、手元のmacbookで。

t_seq.py

def count(n):
    while n > 0: n -= 1

count(100000000)
count(100000000)

t_thread.py

from threading import Thread

def count(n):
    while n > 0: n -= 1

t1 = Thread(target=count,args=(100000000,))
t1.start()
t2 = Thread(target=count,args=(100000000,))
t2.start()
t1.join()
t2.join()

t_mprocessing.py

from  multiprocessing import Process

def count(n):
    while n > 0:
        n -= 1

t1 = Process(target=count,args=(100000000,))
t1.start()
t2 = Process(target=count,args=(100000000,))
t2.start()
t1.join()
t2.join()

結果

$ time python t_seq.py;time python t_thread.py;time python t_mprocessing.py

real    0m17.032s
user    0m16.886s
sys 0m0.067s

real    0m37.139s
user    0m29.756s
sys 0m23.039s

real    0m8.777s
user    0m16.973s
sys 0m0.083s

Clojureのほう

user=> (defn countdown [n] (when (pos? n) (recur (dec n))))

#'user/countdown
user=> user=> (time (doall (pmap countdown (repeat 2 100000000))))
"Elapsed time: 8164.085 msecs"
(nil nil)

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021