Created
May 1, 2014 03:11
-
-
Save seanjensengrey/298a7870d4871ca8db35 to your computer and use it in GitHub Desktop.
100 threads that append a random number to a list 20000 times
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
import thread | |
import time | |
import random | |
big_list = [] | |
thread_status = {} | |
def appendo(n): | |
thread_status[thread.get_ident()] = "running" | |
for x in xrange(n): | |
big_list.append(random.randint(1,5000)) | |
thread_status[thread.get_ident()] = "stopped" | |
tl = [ thread.start_new_thread(appendo,(20000,)) for x in range(100) ] | |
print len(tl) | |
time.sleep(0) | |
while 1: | |
if 'running' in thread_status.values(): | |
time.sleep(0) | |
else: | |
break | |
print "the size of big_list", len(big_list) | |
print "sum: ", sum(big_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thread Overhead
I ran the above script for two invocations, one with 100 threads writing 20k random ints to a list the other with 1 thread writing 2M random ints. So that same amount of work, one thread verses 100 threads.
cpython
pypy