Skip to content

Instantly share code, notes, and snippets.

@natemurthy
Created May 30, 2014 06:55
Show Gist options
  • Save natemurthy/f592dc790016957f4136 to your computer and use it in GitHub Desktop.
Save natemurthy/f592dc790016957f4136 to your computer and use it in GitHub Desktop.
# 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