#100DaysOfCode in Python Transcripts
Chapter: Days 49-51: Measuring performance
Lecture: Concepts: Profiling
0:00 Let's review some of the concepts around measuring
0:02 performance with profiling and Python's cProfile module.
0:07 Now from any project, regardless of your editor,
0:11 if you just want to measure the overall performance
0:14 of your app, this is probably the best thing you can do.
0:17 python -m cProfile,
0:20 so run Python, make sure that's the right version.
0:23 Instruct it to run the module cProfile, capital P,
0:28 and while you're at it, the most useful one
0:30 is sorting by cumtime.
0:32 So do a -S, cumtime and then give it
0:35 to a entry point into your Python program to run.
0:38 Here program notqi, and you automatically get
0:40 that nice output that you can start working with it,
0:43 iterating on.
0:45 One, to run just a section of code
0:47 there's two options, there's one I'm showing you here
0:50 and you could also create a unit test
0:53 that just run that code and then
0:55 run the unit test with profiling.
0:57 But we're going to focus on the more general case here.
0:59 So what you do is you import the profiler,
1:02 and tell it to start up disabled
1:04 stop tracking anything, just import yourself
1:07 and go from there.
1:09 Run whatever startup code you got to do
1:11 to get into the state you want to profile,
1:14 and then enable profiling, run your code
1:17 and go back and disable it.
1:19 And in our example you saw we actually moved the reporting
1:21 outside of this block and the data generation
1:25 within it so that we could measure really precisely
1:28 just the part we were working on.
1:30 Some other stuff here at the end
1:31 you probably don't care about.
1:32 And at some point you want to see the stats
1:34 so you'll say, profiler.print_stats.