Created
February 15, 2017 11:00
-
-
Save Aluriak/01c3d100cb44ef048c00854c6f439642 to your computer and use it in GitHub Desktop.
Compare intention lists with condition on a set
This file contains 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
from timeit import timeit | |
print('First case: with complex set building notation') | |
def integrated_complex(): | |
N = 1000 | |
return [e for e in range(N) if e in {i for i in range(N) if i%2}] | |
def decoupled_complex(): | |
N = 1000 | |
s = {i for i in range(N) if i%2} | |
return [e for e in range(N) if e in s] | |
for func in (integrated_complex, decoupled_complex): | |
print(func.__name__, timeit(func, number=10)) | |
print('Second case: with simple set building notation') | |
def integrated_simple(): | |
N = 1000 | |
return [e for e in range(N) if e in {1, 2, 3, 4, 5}] | |
def decoupled_simple(): | |
N = 1000 | |
s = {1, 2, 3, 4, 5} | |
return [e for e in range(N) if e in s] | |
for func in (integrated_simple, decoupled_simple): | |
print(func.__name__, timeit(func, number=10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results on my machine :
Therefore, it seems that sets in
if
expression are built at each loop.