Note: As mentioned in the comments, the following applies to the paid version of PyCharm:
If using 3.x (don't know about 2.x), I'll add to shafeen's answer and make it more PyCharm specific as per the original post. This also works better for web applications or larger applications versus simple command line programs where printing the output to stdout might be okay (still better to be able to sort different ways through PyCharm's viewer).
Indeed, do as suggested by instantiating Profile and enabling and disabling as needed. To make that useful though, you'll want to save this to a file.
- In an outer section of your code, instantiate Profile.
- In the inner section of your code, do your profiling.
- Now, call pr.dump_stats('profile.pstat')
You now have a profile file that you would like to examine. Go to Tools|Open CProfile snapshot. Select profile.pstat and now you can view and sort by different headings as desired.
Summary
import cProfile as profile
# In outer section of code
pr = profile.Profile()
pr.disable()
# In section you want to profile
pr.enable()
# code of interest
pr.disable()
# Back in outer section of code
pr.dump_stats('profile.pstat')
Open file in PyCharm's CProfile viewer.