Effective PyCharm Transcripts
Chapter: Performance and profiling
Lecture: Profiling the slow app
0:00 Now that you're familiar with this program,
0:01 it's time to profile it. But one quick hang up here that I just got
0:06 to tell you about. There was a bug in one part of the profile visualization
0:11 that was in the latest version of PyCharm.
0:12 Not the EAP but the actual shipping one.
0:15 So I had to come back here and roll back to two 2021.1.3
0:19 . I'm sure by the time you get this course it'll be fixed,
0:22 it's already been addressed. The only reason I mentioned this is for whatever reason to
0:27 lay out of my toolbar changed.
0:29 So beware, it probably will be back over here for you.
0:32 Now. What we want to do is we want to profile this.
0:35 So you're familiar with running the program and you're familiar with debugging it.
0:40 But if we go over here we can click this and say profile.
0:43 So long as you have a run configuration,
0:46 you can just press this button and it will run and use python's c profile to
0:51 figure out where we're spending our time.
0:52 Let's go. So they're ran like usual.
1:02 But now we got this cool report,
1:03 let's check it out over here.
1:05 It shows you where you're spending your time and it uses this own time by default
1:10 And what that means is if a function is being called by another function,
1:15 how much time are you spending at that level of the call stack?
1:19 It doesn't tell you how much time that function takes to run because if it calls
1:23 other functions itself, it ignores the time that is down calling those other functions.
1:28 I find this to be not a very helpful terms.
1:31 So what I want to know is how long did the function take to run,
1:34 including things that might have had to do while it was running.
1:39 So that's this one here. So we're going to sort it by the top and
1:42 now over here we've got the interesting things.
1:44 We've got program, obviously this one took the longest because that's the start up.
1:49 We've got main and we've got to go and then we started getting into the interesting
1:53 things. So the pieces that we worked on is we have compute analytics.
1:58 Remember up here we have search get records,
2:03 we have compute analytics. So here's our computer analytics.
2:06 That one is the slowest. We spent 3.3,
2:10 seconds there And 68% of the time.
2:14 We have our search where we spent 353 milliseconds,
2:18 only 7% of the time and get records the database.
2:22 One was the third where we spent 1.2 seconds or 23% of the time.
2:27 So this is helpful. And you can even do cool things like let's go over
2:30 to this function if I double click it,
2:32 it takes us right there, which is fantastic.
2:35 And if you right click you can navigate the source or show on call graph.
2:40 Let's check out the call graph.
2:41 This is actually probably the most useful as your first trying to understand what performance means
2:47 See how useful and meaningful.
2:51 No, wait, this is not very helpful.
2:52 We gotta zoom in. Okay,
2:53 let's zoom in and we'll go over here and I got to find your way to
2:58 the bottom. So notice there's all this stuff around.
3:03 Find and load and frames and what not that's not really relevant to us.
3:08 We can't control that. That's just python starting up.
3:11 So we want to start with when is our script running here?
3:15 They wouldn't come over and see that We're going to run program and notice it's 4.9
3:20 seconds or the total time, but 0% of its own time.
3:24 Same for main and same for go.
3:25 This is like basically just orchestrating these three functions.
3:29 Right? And again, they don't take much of their own time either.
3:32 They're just calling a bunch of things that themselves take time.
3:35 Notice the colors, red means really slow,
3:38 relatively speaking. Green means pretty fast,
3:41 relatively speaking and we're still spending a second here,
3:44 but it's still better than the five.
3:48 And again, green. So what we can do is we can quickly get a
3:51 look here and say, all right,
3:52 well, where are we spending our time?
3:54 What is slow? We should probably start right there.
3:58 That one is the slowest. So if we could optimize that,
4:01 it's going to have the biggest impact.
4:03 For example, if we could make a search instant if somehow we could make ping
4:08 time on the internet go away,
4:10 if we could make the talk python server was turn instantly to us,
4:14 what benefit would we have working on this function?
4:18 353 milliseconds out of 4800. So we can go from 4.8 Down to 4.5 seconds
4:26 So no matter how much we work on this function,
4:30 it's not going to get faster than,
4:32 you know, 7% faster. Right?
4:34 So you really want to think about,
4:35 you know, where can I spend my time and where is it meaningful to try
4:39 to improve this? A lot of times people work on code and they say,
4:42 well this could be faster if we do some crazy things do really optimize it.
4:46 That makes the code harder to maintain and understand,
4:49 but it will make it faster.
4:50 Just remember You're only going to ever get 7.2% improvement here.
4:55 So also think about maintenance, readability,
4:58 all those kind of things. All right.
5:00 So use this to kind of initially guide where you want to go and again we
5:05 can click over here and navigate the source.
5:07 Boom, that's probably what we should be working on first.
5:11 Another thing to notice is, here's our result,
5:14 this course 6 P stat that means it's the sixth time I ran it because I
5:18 was fiddling around with the previous version when it had the bug,
5:22 I'm gonna put that one on the left and when we run it again we've made
5:27 no changes yet. But we will and when we run it and we make those
5:30 changes we're going to get another one.
5:32 So we'll be able to do things like come over here and say well let's sort
5:35 this like that and sort this one like that and then we could just cycle between
5:40 them. Right? That's uh that's one way we can kind of get a sense
5:44 for how are we making improvements?
5:45 How was it before? And how was it after?
5:48 I'm going to make a point to leave this course 6p stat over here so that
5:53 I can have it as a reference as we improve throughout this chapter.