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