Created
May 30, 2014 06:55
-
-
Save natemurthy/f592dc790016957f4136 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# seq.py | |
import time | |
def countdown(n): | |
while n > 0: | |
n -= 1 | |
COUNT = 50000000 | |
start = time.time() | |
countdown(COUNT) | |
end = time.time() | |
print(end-start) | |
# par.py | |
from threading import Thread | |
import time | |
def countdown(n): | |
while n > 0: | |
n -= 1 | |
COUNT = 50000000 | |
t1 = Thread(target=countdown, args=(COUNT/2,)) | |
t2 = Thread(target=countdown, args=(COUNT/2,)) | |
start = time.time() | |
t1.start(); t2.start() | |
t1.join(); t1.join() | |
end = time.time() | |
print(end-start) | |
# spin.py | |
# spin uselessly | |
while True: | |
pass | |
# parspin.py | |
from threading import Thread | |
from subprocess import Popen | |
import time | |
def countdown(n): | |
while n > 0: | |
n -= 1 | |
COUNT = 50000000 | |
p = Popen(['python','spin.py']) | |
t1 = Thread(target=countdown, args=(COUNT/2,)) | |
t2 = Thread(target=countdown, args=(COUNT/2,)) | |
start = time.time() | |
t1.start(); t2.start() | |
t1.join(); t1.join() | |
end = time.time() | |
p.terminate() | |
print(end-start) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment