Skip to content

Instantly share code, notes, and snippets.

@markrwilliams
Created November 19, 2015 03:52
Show Gist options
  • Save markrwilliams/dea42ec7914e4d54fb23 to your computer and use it in GitHub Desktop.
Save markrwilliams/dea42ec7914e4d54fb23 to your computer and use it in GitHub Desktop.
from threading import RLock
class T(object):
def __init__(self):
self.lock = RLock()
def _meth(self):
pass
@staticmethod
def _smeth():
pass
def nometh(self):
with self.lock:
pass
def instmeth(self):
with self.lock:
self._meth()
def statmeth(self):
with self.lock:
self._smeth()
t = T()
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.nometh()'
100000 loops, best of 3: 1.84 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.nometh()'
100000 loops, best of 3: 1.87 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.nometh()'
100000 loops, best of 3: 1.81 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.instmeth()'
100000 loops, best of 3: 1.92 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.instmeth()'
100000 loops, best of 3: 2.05 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.instmeth()'
1000000 loops, best of 3: 1.96 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.statmeth()'
100000 loops, best of 3: 1.99 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.statmeth()'
100000 loops, best of 3: 2.01 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.statmeth()'
100000 loops, best of 3: 1.98 usec per loop
mrw@hammal:/tmp$ python -m timeit -s 'from locktest import t' 't.statmeth()'
100000 loops, best of 3: 1.99 usec per loop
mrw@hammal:/tmp$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment