Created
August 3, 2013 20:39
-
-
Save dgabriele/6147894 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
import random | |
import time | |
import threading | |
from itertools import product | |
from Queue import Queue | |
import numpy | |
class Thing(object): | |
def __init__(self, key): | |
self.key = key | |
class test(object): | |
def __init__(self, n_threads=2): | |
self.n_threads = n_threads | |
def __call__(self, f): | |
def g(*args, **kwargs): | |
t0 = time.time() | |
threads = [] | |
for i in range(self.n_threads): | |
t = threading.Thread(target=f) | |
t.start() | |
threads.append(t) | |
for t in threads: | |
t.join() | |
t1 = time.time() | |
return t1-t0 | |
return g | |
table = {} | |
crumbs = ['dfgdg', 'sdf', 'brtq', 'poxcva', 'ad', 'dgwev', 'xvqwrxs'] | |
locks = [threading.Lock() for i in range(16)] | |
global_lock = threading.Lock() | |
## Build object lookup table | |
for i,p in enumerate(product(crumbs, crumbs, crumbs)): | |
k = ':'.join(p) | |
table[k] = Thing(k) | |
@test(n_threads=4) | |
def f1(): | |
for p in product(crumbs, crumbs, crumbs): | |
k = ':'.join(p) | |
with locks[hash(k)&15]: | |
t = table[k] | |
@test(n_threads=4) | |
def f2(): | |
for p in product(crumbs, crumbs, crumbs): | |
k = ':'.join(p) | |
with global_lock: | |
#t = Thing(k) | |
t = table[k] | |
@test(n_threads=4) | |
def f3(): | |
for p in product(crumbs, crumbs, crumbs): | |
k = ':'.join(p) | |
t = Thing(k) | |
f21 = [] | |
f31 = [] | |
for i in range(10): | |
f21.append(f2()/f1()) | |
f31.append(f3()/f1()) | |
print "speedup of lock table over global lock: {0:.2f}x".format(numpy.mean(f21)) | |
print "speedup of lock table over re-instantiation: {0:.2f}x".format(numpy.mean(f31)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment