<jupyter_start><jupyter_text>Getting the top K sorted elements Time and space complexity<jupyter_code>import heapq
%load_ext memory_profiler
# generator of this size
n = 20000000
k = 10
def get_it():
    return (-a for a in range(n))
%%time
sorted(get_it(), key=lambda x: x)[:k]
%%memit
sorted(get_it(), key=lambda x: x)[:k]
%%time
heapq.nsmallest(k, get_it())
%%memit
heapq.nsmallest(k, get_it())<jupyter_output>peak memory: 118.01 MiB, increment: 0.00 MiB<jupyter_text>Sorting tuples<jupyter_code>def get_it_tuple():
    return ((a, -a) for a in range(n))
%%timeit
sorted(get_it_tuple(), key=lambda x: x[1])[:10]
%%memit
sorted(get_it_tuple(), key=lambda x: x[1])[:10]
%%timeit
heapq.nsmallest(k, get_it_tuple(), key=lambda x: x[1])
%%memit
heapq.nsmallest(k, get_it_tuple(), key=lambda x: x[1])<jupyter_output>peak memory: 42.05 MiB, increment: 0.00 MiB
{'fraction_non_alphanumeric': 0.1647196261682243, 'fraction_numerical': 0.038551401869158876, 'mean_word_length': 6.862385321100917, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '405394', 'license': 'MIT', 'n_tokens_mistral': 383, 'n_tokens_neox': 362, 'n_words': 88, 'path': 'python/getting-the-top-k-sorted-elements-from-a-generator.ipynb', 'repo_name': 'ashishpatel26/learning', 'size': '856'}