Skip to content

Instantly share code, notes, and snippets.

@koyachi
Created December 2, 2012 21:02
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 koyachi/4191054 to your computer and use it in GitHub Desktop.
Save koyachi/4191054 to your computer and use it in GitHub Desktop.
# https://www.ibm.com/developerworks/jp/linux/library/l-pythrd/
from __future__ import generators;
import time;
threads = []
TIMES = 100000
def stringops():
for n in xrange(TIMES):
s = "Mary had a little lamb"
s = s.upper()
s = "Mary had a little lamb"
s = s.lower()
s = "Mary had a little lamb"
s = s.replace('a', 'A')
def scheduler():
for n in xrange(TIMES):
for thread in threads:
thread.next()
def upper():
while 1:
s = "Mary had a little lamb"
s = s.upper()
yield None
def lower():
for n in xrange(TIMES):
s = "Mary had a little lamb"
s = s.lower()
yield None
def replace():
while 1:
s = "Mary had a little lamb"
s = s.replace('a', 'A')
yield None
if __name__ == '__main__':
start = time.clock()
stringops()
looptime = time.clock() - start
print "LOOP TIME: ", looptime
# global threads
threads.append(upper())
threads.append(lower())
threads.append(replace())
start = time.clock()
scheduler()
threadtime = time.clock() - start
print "THREAD TIME: ", threadtime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment