00:0.44 Let's review the core concepts around profiling with PyCharm and python.
00:5.84 Once you have a run configuration setup in PyCharm,
00:9.22 you just go over and hit the profile buttons,
00:11.9 you can right click on the thing instead of saying run in this case program you
00:15.44 can say profile program or you can hit that little clock stopwatch looking thing next to
00:21.65 the play button and debug button that you're familiar with.
00:24.94 Once you click profile you'll see that PyCharm automatically starts up what's called C profile
00:29.55 This is a profiler within python,
00:32.92 that is a C based one.
00:34.84 It's pretty low overhead and works really well for understanding where our program is spending its
00:39.61 time once it's done running once you exit the program or if it's some kind of
00:46.15 program that just runs and then stops,
00:48.53 you'll get this 'pstat' file opened up here and it opens up to the
00:53.22 statistics page where you can basically sort through and look at the different elements.
00:58.36 It starts out by showing your own time.
01:0.24 I find that not to be very useful.
01:2.14 I would focus in on the overall time spent in any given part of our program
01:7.64 for our program, we saw compute analytics learn run query search,
01:12.75 get records. All of these were the things that were interesting for us to look
01:16.91 at. My personal favorite way to get a quick overview of where we're spending our
01:21.9 time is actually go over to the call graph and have this visual representation.
01:26.64 I love how the colors in the flow,
01:28.99 show your where you're spending your time,
01:30.81 for example, we're calling. Go and go is calling Get records and get records
01:34.19 Calling. Run query that right now is the path that we really need to
01:38.41 be worried about when you're back over in that spreadsheet looking thing.
01:41.43 You don't see that nearly as obviously as you do here.
01:45.74 So we are called program it starts up program calls main,
01:48.98 main calls, go go calls,
01:50.58 three functions, compute analytics. Get records and search.
01:55.74 And again the color matters when we see green.
01:59.55 Well that means that this is fast enough when we see yellow relatively speaking,
02:4.14 it's not horrible necessarily compared to the other parts of the program,
02:8.34 but it's something you should pay attention to when it's red.
02:11.67 You definitely should, you know,
02:13.33 focus in on that and see what's going on.
02:15.32 Look at this graph, The colors and the flow are actually and really,
02:18.95 really good for you to be able to just jump right in and figure out what's
02:22.16 going on. We can also navigate from both the statistics and call graph view.
02:28.64 If we go to the statistics one,
02:30.83 we can right click and say show on the call graph and jump right into that
02:33.96 picture where we should be or we can say navigate over to the source or even
02:38.6 double clicking get records right here.
02:40.05 We'll just take us there when we're over in the call graph we can select and
02:45.06 then right click one of these boxes and say navigate to source. In a final word
02:50.43 of warning as we close out this section.
02:52.79 This chapter on profiling, profilers and debuggers.
02:57.03 But profilers maybe even more so can have significant effects that actually alter how our program
03:3.64 behaves in quantum mechanics. We have this really weird principle that if you observe something
03:8.79 you may actually change it. The act of observing it may be changing it and
03:13.93 profilers have this sort of quantum mechanics feel to them there are certain parts of your
03:19.06 program that when run outside of a profiler it could be medium slow but if you
03:24.55 run it in the profiler it could actually be a lot slower and the profiler disabled
03:29.0 This is really slow and you know,
03:30.88 in practice that may not always be true.
03:33.64 One of those scenarios is where you're doing many,
03:35.66 many, many loops in python code versus calling something external like a database query or
03:41.09 calling requests. The overhead of calling requests or a database query once is nearly zero
03:46.71 But if you're calling a loop thousands and thousands of times there's a little bit
03:51.06 of overhead for each function call from the profiler and that can add up.
03:55.4 So just be aware that this might not be 100%.
03:59.13 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.
