Created
December 2, 2014 17:11
-
-
Save bmcorser/c1cf4d30cc51c604baba to your computer and use it in GitHub Desktop.
`set` vs `dict`
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 operator import itemgetter | |
data = [ | |
{'id': 1}, | |
{'id': 2}, | |
{'id': 3}, | |
{'id': 4}, | |
] | |
def one_line(): | |
return set(map(itemgetter('id'), data)) | |
def six_lines(): | |
sequence_ids = {} | |
for dna_molecule in data: | |
id = dna_molecule['id'] | |
if id in sequence_ids: | |
continue | |
sequence_ids[id] = id | |
return sequence_ids.keys() | |
if __name__ == '__main__': | |
import timeit | |
print('one_line()') | |
print(timeit.timeit("one_line()", setup="from __main__ import one_line")) | |
print('six_lines()') | |
print(timeit.timeit("six_lines()", setup="from __main__ import six_lines")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment