Created
January 22, 2010 22:50
-
-
Save mahmoudimus/284253 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
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from itertools import chain | |
def test_really_in(y, xs): | |
for x in xs: | |
if y is x: return True | |
return False | |
def test_filtered_really_in(y, xs): | |
return filter(lambda e: e is y, xs) | |
def test_list_really_in(y, xs): | |
return [x for x in xs if y is x] | |
def test_any_suggestion(y, xs): | |
return any(e is y for e in xs) | |
if __name__ == '__main__': | |
from timeit import Timer | |
itercnt = 10000 | |
expression = "%s(True, chain(xrange(0, 1000), [True]))" | |
impst = "from __main__ import %s, chain" | |
funcs = [x for x in globals().keys() if x.startswith('test_')] | |
for f in funcs: | |
t = Timer(expression % f, impst % f) | |
print f, t.timeit(itercnt) | |
Results are: | |
test_filtered_really_in 1.47374796867 | |
test_list_really_in 0.825389862061 | |
test_really_in 0.822808980942 | |
test_any_suggestion 1.01692414284 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment