Skip to content

Instantly share code, notes, and snippets.

@Aluriak
Created February 15, 2017 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aluriak/01c3d100cb44ef048c00854c6f439642 to your computer and use it in GitHub Desktop.
Save Aluriak/01c3d100cb44ef048c00854c6f439642 to your computer and use it in GitHub Desktop.
Compare intention lists with condition on a set
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))
@Aluriak
Copy link
Author

Aluriak commented Feb 15, 2017

Results on my machine :

First case: with complex set building notation
integrated_complex 0.8994400080009655
decoupled_complex 0.0014536359994963277
Second case: with simple set building notation
integrated_simple 0.00047374200039485004
decoupled_simple 0.00047272700066969264

Therefore, it seems that sets in if expression are built at each loop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment