I have code to measure time correctly with using multi-line string literal and I want to make a memory measurement but because I use multi-line to measure time, I cannot use the functions that I have to use during memory measurement.
import timeitfrom memory_profiler import memory_usage# Define setup code (importing modules and defining functions)setup_code = """from Crypto.Cipher import AESfrom Crypto.Random import get_random_bytesfrom Crypto.Util.Padding import pad, unpaddef encrypt_data(data, key): cipher = AES.new(key, AES.MODE_ECB) padded_data = pad(data, AES.block_size) encrypted_data = cipher.encrypt(padded_data) return encrypted_datadef encrypted_data_function(): key = get_random_bytes(16) data = b'Secret Data' encrypted_data = encrypt_data(data, key) # Encrypt the data return encrypted_data"""# Define the function to be measureddef measure_encrypted_data_function(): return encrypted_data_function()# Measure timetime_result = timeit.timeit(stmt="encrypted_data_function()", setup=setup_code, number=1000)# Measure memory usagememory_result = memory_usage(measure_encrypted_data_function, setup=setup_code, max_usage=True)print("Average Encryption Time:", time_result)print("Maximum Memory Usage:", memory_result[0], "MB")
At this part
# Define the function to be measureddef measure_encrypted_data_function(): return encrypted_data_function()
The code doesn't see encrypted_data_function
because it's inside the multi-line string; how can I make it seen by it?