Skip to content

Instantly share code, notes, and snippets.

@seanjensengrey
Created May 1, 2014 03:11
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 seanjensengrey/298a7870d4871ca8db35 to your computer and use it in GitHub Desktop.
Save seanjensengrey/298a7870d4871ca8db35 to your computer and use it in GitHub Desktop.
100 threads that append a random number to a list 20000 times
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)
@seanjensengrey
Copy link
Author

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

$ time python append_list2.py 
100
the size of big_list 2000000
sum:  4995798476

real    0m6.199s
user    0m5.706s
sys 0m2.238s
$ time python append_list2.py 
1
the size of big_list 2000000
sum:  4998149729

real    0m5.335s
user    0m4.863s
sys 0m1.866s

pypy

$ time pypy append_list2.py 
100
the size of big_list 2000000
sum:  5003485315

real    0m0.328s
user    0m0.230s
sys 0m0.098s
$ time pypy append_list2.py 
1
the size of big_list 2000000
sum:  5002606777

real    0m0.291s
user    0m0.203s
sys 0m0.088s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment