Skip to content

Instantly share code, notes, and snippets.

@pjz
Last active April 11, 2016 17:28
Show Gist options
  • Save pjz/8bd1408c22f6426285e2df3fb155b270 to your computer and use it in GitHub Desktop.
Save pjz/8bd1408c22f6426285e2df3fb155b270 to your computer and use it in GitHub Desktop.
import timeit
class Foo: pass
foo = Foo()
sfoo = 'foo' * 333
sfo = 'foo' * 332
dfoo = { 'foo': 1, 'bar': 2, 'baz': 3, 'bal': 4 }
setfoo = set(['foo', 'bar', 'baz', 'bal'])
def test_isinstance():
if isinstance(foo, Foo): pass
def test_typecomp():
if type(foo) == Foo: pass
def test_uninterned_stringcomp():
if sfoo == sfo + "foo": pass
def test_interned_stringcomp():
if sfoo == sfoo: pass
def test_sub_stringcomp():
if sfoo.startswith(sfo): pass
def test_dictmember():
if 'foo' in dfoo: pass
def test_multidictmember():
if 'foo' in dfoo and 'bar' in dfoo and 'baz' in dfoo: pass
def test_setmember():
if 'foo' in setfoo: pass
multisearch = set(('foo', 'bar', 'baz'))
def test_multisetmember():
if multisearch.issubset(setfoo): pass
if __name__ == '__main__':
for testname in ('test_isinstance',
'test_typecomp',
'test_uninterned_stringcomp',
'test_interned_stringcomp',
'test_sub_stringcomp',
'test_dictmember',
'test_multidictmember',
'test_multisetmember'
):
result = timeit.timeit(testname + "()", setup="from __main__ import " + testname)
print("Testing %s : %f" % (testname, result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment