#100DaysOfCode in Python Transcripts
Chapter: Days 49-51: Measuring performance
Lecture: Concepts: Profiling

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Let's review some of the concepts around measuring performance with profiling and Python's cProfile module.
0:08 Now from any project, regardless of your editor, if you just want to measure the overall performance
0:15 of your app, this is probably the best thing you can do. Python -m cProfile, so run Python, make sure that's the right version.
0:24 Instruct it to run the module cProfile, capital P, and while you're at it, the most useful one is sorting by cumtime.
0:33 So do a -S, cumtime and then give it to a entry point into your Python program to run. Here program notqi, and you automatically get
0:41 that nice output that you can start working with it, iterating on. One, to run just a section of code
0:48 there's two options, there's one I'm showing you here and you could also create a unit test that just run that code and then
0:56 run the unit test with profiling. But we're going to focus on the more general case here. So what you do is you import the profiler,
1:03 and tell it to start up disabled stop tracking anything, just import yourself and go from there. Run whatever startup code you got to do
1:12 to get into the state you want to profile, and then enable profiling, run your code and go back and disable it.
1:20 And in our example you saw we actually moved the reporting outside of this block and the data generation
1:26 within it so that we could measure really precisely just the part we were working on. Some other stuff here at the end you probably don't care about.
1:33 And at some point you want to see the stats so you'll say, profiler.print_stats.


Talk Python's Mastodon Michael Kennedy's Mastodon