#100DaysOfCode in Python Transcripts
Chapter: Days 49-51: Measuring performance
Lecture: Demo: Fine-tuning collection with the API

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Alright, let's go back to our code here and we're going to do a little bit of work with the API. So what we can do, is we can come up here
0:07 and say we're going to try to, as much as possible, ignore the start up time and all those kinds of things
0:13 and we just want to measure all our important code. So what we're going to do is import cProfile and this is not great, but, before we even
0:21 try to go and import this we're going to create a profiler and disable further profiling. So we'll go like so. There we go, we're going to say
0:33 profiler disable and probably we'll just actually take this code out once we're done playing around with it, 'cause, you know,
0:39 these are supposed to go to the top. But I don't want to time that stuff. We're going to say, disable.
0:44 What we want to do is we want to time this method, I want to time this one, and this one, and we're just going to straight up time it at first
0:49 and then we're going to reorganize it so we get a better look at it here. Alright so let's go to our profiler and say enable.
0:56 And then as soon as we're done down here, we'll go down and say profiler disable. Okay. Now if we run this
1:04 are we going to see some great profiler output? Eh, probably not, let's try. Well we ran it 25 times, that was cool. But nah, where'd our stats go?
1:14 None. Okay, so what we actually need to do down here at the end is say profiler.printstats and this will give us basically
1:25 the same graph as we had before. There it is. Now, of course, it's sorting by heck I don't actually know what it's sorting by. But not what we want.
1:33 So we come down here and say sort. Now this is annoying, I guess I'll say it that way. It says the sort is an integer and its default value is -1.
1:44 Do you know what you put here? Cumtime as a string. Yeah let's go ahead and tell PyCharm that's spelled correctly.
1:50 That's what it is, that's how it works. Okay, so down here, now you can see the cumulative time is descending.
1:57 It looks like we're sorting correctly there. We've had 115,000 function calls. That's non-trivial, apparently. Look at this, look how much cleaner
2:07 and realistic this looks. Alright, we're spending time in research and net, and parse row, this is kind of the whole startup time bit.
2:13 This next stuff, this is definitely in there. We're spending some time in sorted. That's pretty cool. And here we have our three,
2:20 our hot days, wet days, and cold days. Okay, that's pretty cool, and then here you can see some of these lambdas, or our sort functions
2:28 that we're passing along in research, and so on. So this gives us a much more clean and pure view of what's going on here.
2:36 Let's actually crank this up to 100. Just to make it stand out a little bit more. Here we go. So now we're spending a decent amount of time in
2:47 these places, and here we're spending like, not quite 20 milliseconds in the three, data reporting sections. Okay this is all well and good
2:57 and these numbers right here, the stuff I've highlighted, is great. However, this method here, it's kind of hiding, this main, where's main?
3:09 Main's not showing up because we didn't, we didn't call it directly, we basically disabled profiling but there's still some stuff going on here
3:15 like this looping, and this numarray and this string formatting, all this junk is still being profiled.
3:21 We're going to use the API to clean that up as well.


Talk Python's Mastodon Michael Kennedy's Mastodon