Created
November 25, 2016 17:28
-
-
Save Aluriak/89015cbaa4965c520a373d9287a6359a to your computer and use it in GitHub Desktop.
Demonstration of the absence of optimisation on intension's if clause
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
""" | |
Demonstration of the absence of optimisation on following intension syntax: | |
n for n in data if no not in set(toignore) | |
Here, `set(toignore)` is built at each loop, | |
leading to the following output of this code: | |
one: 3.593955495998671 | |
two: 3.5888819139945554 | |
tee: 0.007060877003823407 | |
fur: 0.006962022001971491 | |
It is also shown that frozenset set are slightly faster than sets. | |
""" | |
import timeit | |
import random | |
from functools import partial | |
FULL_DATA = tuple(range(10000)) | |
SUBSIZE = 1000 | |
SUBSET = random.sample(FULL_DATA, SUBSIZE) | |
def one(data, toignore): | |
return tuple(n for n in data if n not in set(toignore)) | |
def two(data, toignore): | |
return tuple(n for n in data if n not in frozenset(toignore)) | |
def tee(data, toignore): | |
toignore = set(toignore) | |
return tuple(n for n in data if n not in toignore) | |
def fur(data, toignore): | |
toignore = frozenset(toignore) | |
return tuple(n for n in data if n not in toignore) | |
if __name__ == "__main__": | |
for method in (one, two, tee, fur): | |
print(method.__name__ + ':', timeit.timeit(partial(method, data=FULL_DATA, toignore=SUBSET), number=10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment