<jupyter_start><jupyter_text>Python Optimizations: Peephole- another variety of optimizations occuring at compile timePeephole optimizations- constant expressions: numeric calculations  24 * 60 --> 1440- short sequences length: (1, 2) * 3  --> (1, 2, 1, 2, 1, 2), strings up to 20 characters- membership tests: if e in list, list --> tuple; sets --> frozensets<jupyter_code>def my_func():
    a= 24 * 60
    b = (1, 2) * 5
    c = "abc" * 3
    d = "ab" * 11
    e = "The quick brown fox" * 5
    f = ["a", "b"] * 3<jupyter_output><empty_output><jupyter_text>Looking at function constants<jupyter_code>my_func.__code__.co_consts<jupyter_output><empty_output><jupyter_text>Looking at function membership test constants<jupyter_code>def my_func(e):
    if e in [1, 2, 3]:  # peephole optimization: list to tuple
        pass
my_func.__code__.co_consts<jupyter_output><empty_output><jupyter_text>Membership comparison<jupyter_code>import string
import time
string.ascii_letters
char_list = list(string.ascii_letters)
char_tuple = tuple(string.ascii_letters)
char_set = set(string.ascii_letters)
print(char_list)
print(char_tuple)
print(char_set) # unordered
def membership_test(n, container):
    for i in range(n):
        if 'z' in container:
            pass
start = time.perf_counter()
membership_test(10_000_000, char_list)
end = time.perf_counter()
print("list: ", end - start)
start = time.perf_counter()
membership_test(10_000_000, char_tuple)
end = time.perf_counter()
print("tuple: ", end - start)
start = time.perf_counter()
membership_test(10_000_000, char_set)
end = time.perf_counter()
print("set: ", end - start)<jupyter_output>set:  0.7203901440370828
{'fraction_non_alphanumeric': 0.11899038461538461, 'fraction_numerical': 0.045072115384615384, 'mean_word_length': 5.054545454545455, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '213268', 'license': 'MIT', 'n_tokens_mistral': 644, 'n_tokens_neox': 580, 'n_words': 147, 'path': 'python-deepdive/deepdive1/section03/section_03_27.ipynb', 'repo_name': 'erictapia/devstacklab', 'size': '1664'}