Created
December 2, 2012 21:02
-
-
Save koyachi/4191054 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# 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