PyCharm Profiler: Make your code run faster

Опубликовано: 02 Август 2021
на канале: buckmasterinstitute
11,648
70

Created and recorded by Aman Chhina. July 2021

Overview
1. Introduction
2. What is the problem we are trying to solve and what is the naive way to solve it.
3. How a profiler is better than the naive way
4. Explain example code with methods
5. Run profiler and identify bottleneck
6. Explore statistics and call graph to remove bottlenecks
7. Review
8. Outro

- Hello everybody today we will be using pycharms profiler to make our code run faster.
- If we are in a situation where we'd like to optimize our code to make it run faster, we might use print statements to identify how long each chunk of code took. However this does not tell us how many calls were made, and gets very clunky and almost impossible with any large sized codebase.
- Incomes a profiler where we get detailed statistics and a call graph for our code executed.
- Lets begin by looking in our example script we have setup. We have these different functions such as sleep_for(), which sleeps for the provided amount of seconds, then we have get_max_n2() and get_max_n() which gets the max of a list in n squared and linear time respectively. get_max_n2() uses a helper function is_smaller() to see if the current number is smaller than another number. We then use these functions at the end of our script.
- Let's take a look at how long this code takes to execute by using the profiler. It took about 3.5 seconds where most of that run time comes from the sleep method, so let us comment that portion of the bottleneck in our code out. Now we see that the second bottle neck is the get_max_n2() function which is doing 1000000 calls to is_smaller() compared to the 1000 calls that get_max_n() is doing to the built in method max().
- So by removing that bottleneck again we now see our code takes 0ms to run and produces the same output as the beginning.
- So to review we used the profiler to identify bottlenecks in our code and removed them, and this would be the same process used in larger more complex scripts/codebases.
- If you guys found this video educational and useful, please consider liking and subscribing and see you guys next time.