Skip to content

Instantly share code, notes, and snippets.

@kosaki
Created August 23, 2011 01:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kosaki/1164110 to your computer and use it in GitHub Desktop.
Save kosaki/1164110 to your computer and use it in GitHub Desktop.
=== threading ===
#!/usr/bin/python
import sys
import threading
THR_NUM=4
class thr(threading.Thread) :
def run(self) :
val = 0
while val < 100000000 :
val = val + 1
num_of_thr = 0
t = []
while num_of_thr < THR_NUM :
t.append(thr())
num_of_thr = num_of_thr + 1
for x in t :
x.start()
for x in t :
x.join()
===
$ time ./cpubench.py
real 0m58.897s
user 0m57.655s
sys 1m31.172s // !?!?
multiprocessing
==
#!/usr/bin/python
import sys
import multiprocessing
THR_NUM=4
class thr(multiprocessing.Process) :
def run(self) :
val = 0
while val < 100000000 :
val = val + 1
num_of_thr = 0
t = []
while num_of_thr < THR_NUM :
t.append(thr())
num_of_thr = num_of_thr + 1
for x in t :
x.start()
for x in t :
x.join()
==
$ time ./cpubench2.py
real 0m5.519s
user 0m21.829s
sys 0m0.013s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment