Effective PyCharm (2021 edition) Transcripts
Chapter: Performance and profiling
Lecture: Concept: Profiling
0:00 Let's review the core concepts around profiling with PyCharm and python.
0:05 Once you have a run configuration setup in PyCharm,
0:09 you just go over and hit the profile buttons,
0:11 you can right click on the thing instead of saying run in this case program you
0:15 can say profile program or you can hit that little clock stopwatch looking thing next to
0:21 the play button and debug button that you're familiar with.
0:24 Once you click profile you'll see that PyCharm automatically starts up what's called C profile
0:29 This is a profiler within python,
0:32 that is a C based one.
0:34 It's pretty low overhead and works really well for understanding where our program is spending its
0:39 time once it's done running once you exit the program or if it's some kind of
0:46 program that just runs and then stops,
0:48 you'll get this 'pstat' file opened up here and it opens up to the
0:53 statistics page where you can basically sort through and look at the different elements.
0:58 It starts out by showing your own time.
1:00 I find that not to be very useful.
1:02 I would focus in on the overall time spent in any given part of our program
1:07 for our program, we saw compute analytics learn run query search,
1:12 get records. All of these were the things that were interesting for us to look
1:16 at. My personal favorite way to get a quick overview of where we're spending our
1:21 time is actually go over to the call graph and have this visual representation.
1:26 I love how the colors in the flow,
1:28 show your where you're spending your time,
1:30 for example, we're calling. Go and go is calling Get records and get records
1:34 Calling. Run query that right now is the path that we really need to
1:38 be worried about when you're back over in that spreadsheet looking thing.
1:41 You don't see that nearly as obviously as you do here.
1:45 So we are called program it starts up program calls main,
1:48 main calls, go go calls,
1:50 three functions, compute analytics. Get records and search.
1:55 And again the color matters when we see green.
1:59 Well that means that this is fast enough when we see yellow relatively speaking,
2:04 it's not horrible necessarily compared to the other parts of the program,
2:08 but it's something you should pay attention to when it's red.
2:11 You definitely should, you know,
2:13 focus in on that and see what's going on.
2:15 Look at this graph, The colors and the flow are actually and really,
2:18 really good for you to be able to just jump right in and figure out what's
2:22 going on. We can also navigate from both the statistics and call graph view.
2:28 If we go to the statistics one,
2:30 we can right click and say show on the call graph and jump right into that
2:33 picture where we should be or we can say navigate over to the source or even
2:38 double clicking get records right here.
2:40 We'll just take us there when we're over in the call graph we can select and
2:45 then right click one of these boxes and say navigate to source. In a final word
2:50 of warning as we close out this section.
2:52 This chapter on profiling, profilers and debuggers.
2:57 But profilers maybe even more so can have significant effects that actually alter how our program
3:03 behaves in quantum mechanics. We have this really weird principle that if you observe something
3:08 you may actually change it. The act of observing it may be changing it and
3:13 profilers have this sort of quantum mechanics feel to them there are certain parts of your
3:19 program that when run outside of a profiler it could be medium slow but if you
3:24 run it in the profiler it could actually be a lot slower and the profiler disabled
3:29 This is really slow and you know,
3:30 in practice that may not always be true.
3:33 One of those scenarios is where you're doing many,
3:35 many, many loops in python code versus calling something external like a database query or
3:41 calling requests. The overhead of calling requests or a database query once is nearly zero
3:46 But if you're calling a loop thousands and thousands of times there's a little bit
3:51 of overhead for each function call from the profiler and that can add up.
3:55 So just be aware that this might not be 100%.
3:59 The truth of how much time is being spent by each function but it's still really valuable information help you. Dive in and speed up your app.