Code profiling
By trial, we found that the most expensive part of our code was the string formatting. When your code gets more complex, finding bottlenecks by this method gets harder and at some point becomes impractical.
The solution is to break and analyze small pieces of code. To see how long they take to execute, make a profile of the code.
Python comes with a good profiling tool that automates this process to a certain level. Let's use it on our code to see what it tells us:
Add this import at the beginning of the file:
from timeit import timeit import cProfile
Edit your
if __name__ == '__main__':
block to use the profiler:if __name__ == '__main__': my_list = ['bacon', 'lasagna', 'salad', 'eggs', 'apples'] number = 1000000 profile = cProfile.Profile() profile.enable() for i in range(number): make_list1(my_list) profile.disable() profile.print_stats(sort='cumulative')
Run the code. You should see the profiler statistics on the console. (I suppressed...